码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-08 11:35:14      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

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

bubuko.com,布布扣

2. CodeActivity1和2的代码如下:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

3. 修改Activity1.xaml如下:

bubuko.com,布布扣

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

bubuko.com,布布扣
 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         }
bubuko.com,布布扣

5. 运行。结果如下:

bubuko.com,布布扣

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

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

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

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/ilovewindy/p/3709756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!