C#->控制台工作流->顺序工作流->拖放code->生成事件处理程序
代码如下
代码语言:javascript复制using System;
using System.Workflow.Activities;
namespace wf1
{
public sealed partial class Workflow1: SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(6000);
Console.WriteLine("hello world");
}
}
}
对program.cs内容修改如下
代码语言:javascript复制using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace wf1
{
class Program
{
static void Main(string[] args)
{
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
//工作流完成时触发
workflowRuntime.WorkflowCompleted = delegate(object sender, WorkflowCompletedEventArgs e)
{
//通知waitHandle,释放控制台应用程序
waitHandle.Set();
};
//工作流发生错误时触发
workflowRuntime.WorkflowTerminated = delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
//创建工作流的实例并启动工作流
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(wf1.Workflow1));
instance.Start();
//让控制台等待工作流的完成
waitHandle.WaitOne();
}
Console.ReadKey();
}
}
}