《WF in 24 Hours》读书笔记 - Hour 3(1) - Workflow:添加宿主和事件监听

1. 创建项目组,并添加一个Console Project和Activity Library,在Activity Library的项目中添加CodeActivity1和CodeActivity2,最终结构如下图所示:

2. CodeActivity1和2的代码如下:

复制代码
 1     public sealed class CodeActivity1 : CodeActivity
 2     {
 3         // Define an activity input argument of type string
 4         public InArgument<string> Text { get; set; }
 5 
 6         // If your activity returns a value, derive from CodeActivity<TResult>
 7         // and return the value from the Execute method.
 8         protected override void Execute(CodeActivityContext context)
 9         {
10             Console.WriteLine("before delay: \'{0}\'", DateTime.Now.ToLongTimeString());
11         }
12     }
13 
14     public sealed class CodeActivity2 : CodeActivity
15     {
16         // Define an activity input argument of type string
17         public InArgument<string> Text { get; set; }
18 
19         // If your activity returns a value, derive from CodeActivity<TResult>
20         // and return the value from the Execute method.
21         protected override void Execute(CodeActivityContext context)
22         {
23             Console.WriteLine("after delay: \'{0}\'", DateTime.Now.ToLongTimeString());
24         }
25 }
复制代码

3. 修改Activity1.xaml如下:

4. 修改Console Project的program.cs(注意,需要事先引入WorkflowsProject和System.Activities.dll)

复制代码
 1         static void Main(string[] args)
 2         {
 3             AutoResetEvent syncEvent = new AutoResetEvent(false);
 4 
 5             Activity wf = new WorkflowsProject.Activity1();
 6 
 7             // Create the WorkflowApplication using the desired
 8             // workflow definition.
 9             WorkflowApplication wfApp = new WorkflowApplication(wf);
10 
11             // Handle the desired lifecycle events.
12             wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
13             {
14                 Console.WriteLine("Workflow {0} completed.", e.InstanceId.ToString());
15                 syncEvent.Set();
16             };
17 
18             wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
19             {
20                 Console.WriteLine("Workflow {0} terminated because {1}", e.InstanceId, e.Reason.Message);
21                 syncEvent.Set();
22             };
23 
24             // Start the workflow.
25             wfApp.Run();
26 
27             // Wait for Completed to arrive and signal that
28             // the workflow is complete.
29             syncEvent.WaitOne();
30 
31             // Keep the console screen alive when workflow comnpletes
32             Console.WriteLine("Press enter to continue");
33             Console.Read();
34         }
复制代码

5. 运行。结果如下:

总结:通过此例,我们可以看到通过WorkflowApplication,我们可以在宿主程序中启动Workflow,并且捕捉Workflow的事件。

posted @   GreatK  阅读(297)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示