EventHandlingScopeActivity活动包含一个主线子活动和一组事件处理活动(EventHandlersActivity),它的主线子活动中只能包含一个子活动。它会执行其主子活动比如SequenceActivity。 同时,可能会执行每个EventDrivenActivity,也可能不执行它们,这取决于在SequenceActivity 活动执行时是否发生了它们的事件,当主线活动执行完了整个EventHandlingScopeActivity活动也就结束了。
下面以例子来说明该活动的使用:
1.定义服务接口:
[ExternalDataExchange] public interface IScopeDemo { void Started();
event EventHandler<ExternalDataEventArgs> EventOne; event EventHandler<ExternalDataEventArgs> EventTwo; event EventHandler<ExternalDataEventArgs> EventStop; }
1.1.Started()方法会在工作流开始的时候调用。
1.2.其他三个事件会在HandleExternalEventActivity实例中调用。
2.实现服务接口:
class ScopeDemoService:IScopeDemo { private Guid instanceId; public void Started() { instanceId = WorkflowEnvironment.WorkflowInstanceId; } public event EventHandler<ExternalDataEventArgs> EventOne; public event EventHandler<ExternalDataEventArgs> EventTwo; public event EventHandler<ExternalDataEventArgs> EventStop; public void OnEventOne() { if (EventOne != null) { EventOne(null, new ExternalDataEventArgs(instanceId)); } } public void OnEventTwo() { if (EventTwo != null) { EventTwo(null, new ExternalDataEventArgs(instanceId)); } } public void OnEventStop() { if (EventStop != null) { EventStop(null, new ExternalDataEventArgs(instanceId)); } } }
3.实现工作流,如下图:
3.1.在主线子活动中使用CallExternalMethodActivity活动调用Started方法。
3.2.WhileActivity的条件为!this.IsComplete,当IsComplete为true的时候结束。
3.3.在事件处理活动中我们添加三各EventDriven,每个EventDriven中添加一个HandleExternalEventActivity来
分别调用EventOne,EventTwo和EventStop事件。
工作的完整代码如下:
public sealed partial class ScopeDemoWorkflow: SequentialWorkflowActivity { public ScopeDemoWorkflow() { InitializeComponent(); } private Boolean isComplete = false; public Boolean IsComplete { get { return isComplete; } set { isComplete = value; } } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Console.WriteLine("正在执行主线子活动....."); } private void handleExternalEventActivity1_Invoked(object sender, ExternalDataEventArgs e) { Console.WriteLine("事件响应一"); } private void handleExternalEventActivity2_Invoked(object sender, ExternalDataEventArgs e) { Console.WriteLine("事件响应二"); } private void handleExternalEventActivity3_Invoked(object sender, ExternalDataEventArgs e) { Console.WriteLine("事件响应结束");
IsComplete = true; } }
4.下面来些测试类,代码如下:
public class ScopeTest { private static ScopeDemoService scopeService; public static void Run() { using (WorkflowRuntimeManager manager = new WorkflowRuntimeManager(new WorkflowRuntime())) { AddServices(manager.WorkflowRuntime); manager.WorkflowRuntime.StartRuntime(); Console.WriteLine("开始执行工作流....."); manager.StartWorkflow(typeof(CaryScope.ScopeDemoWorkflow), null); //让主线子活动执行 Thread.Sleep(2000); scopeService.OnEventOne(); Thread.Sleep(100); scopeService.OnEventTwo(); Thread.Sleep(100); //主线子活动执行 Thread.Sleep(3000); //停止工作流 scopeService.OnEventStop(); manager.WaitAll(10000); Console.WriteLine("完成工作流...\n\r"); } } private static void AddServices(WorkflowRuntime instance) { ExternalDataExchangeService exchangeService = new ExternalDataExchangeService(); instance.AddService(exchangeService); scopeService = new ScopeDemoService(); exchangeService.AddService(scopeService); } }
5.执行结果如下图所示:
注意:如果EventHandlingScopeActivity活动的主线子活动执行完成时,已被触发的EventDriven不会因为主线子活动的完成而终止,而是会继续执行完成。