在说wf HadleExternalEventActivity

本文通过一个具体的C#示例介绍了如何利用事件和委托在工作流中进行外部事件的处理。通过定义接口和实现类的方式,展示了如何响应外部触发的事件,并在工作流中调用相应的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

外部触发的事件和工作流在出发之后使用调用的函数是两码事。可以这样认为所谓的接口只是提供了一种的对应关系。
1)首先是接口提供了能干什么?
2)接口的实现类来直接调调用接口中公开的方法,其实这里调用的就是在工作流中invoke中的方法。
所以将所谓的接口只是提供了一种的对应的方法。

例如下面的代码:

Test.cs

namespace HandleExternalEventActivityTest { // Args [Serializable] public class ArgsTest : ExternalDataEventArgs { public ArgsTest(Guid id) : base(id) { } } [Serializable] public class TestImpl : ITest { public event EventHandler<ArgsTest> Agree; public void OnAgree (ArgsTest arg) { if (Agree != null) { Agree(null, arg); } } } }

ITest.cs

namespace HandleExternalEventActivityTest { [ExternalDataExchange] public interface ITest { event EventHandler<ArgsTest> Agree; } }

Program.cs

namespace HandleExternalEventActivityTest { class Program { static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { ExternalDataExchangeService dataExchangeService = new ExternalDataExchangeService(); workflowRuntime.AddService(dataExchangeService); TestImpl service = new TestImpl(); dataExchangeService.AddService(service); AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HandleExternalEventActivityTest.Workflow1)); instance.Start(); ////////////////////////////////////////////////////////////////////////// // 这里时测试 Console.WriteLine("Press any key !"); Console.Read(); service.OnAgree(new ArgsTest(instance.InstanceId)); waitHandle.WaitOne(); } } } }

按照上面的思路其实就是定义了一种对应关系(其实这也是c#中delegate和event关键字的作用)如下:

Agree(null, arg);

实际上就是调用的是:

// workflow1.cs private void OnAgree(object sender, ExternalDataEventArgs e) { Console.WriteLine("Agree !"); }

这里只是为了理解使用‘对应’讲话理解,实际应该参加:

http://msdn.microsoft.com/en-us/library/8627sbea(v=VS.80).aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值