在工作流内部调用宿主的静态方法终止工作流的执行
在工作流内部调用宿主的静态方法终止工作流的执行
业务说明:张三,李四与王五三人审核一份稿件。如果张三审核通过则提交给李四审核,李四审核通过再提交给王五审核。三个人都通过,则稿件审核通过。如果任意一人不通过则工作流结束。
假设现在张三审核通过,李四审核不通过,然后结束了工作流
下面是工作流图:
工作流的内部方法:
public sealed partial class Workflow1: SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}
private void 张三审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("张三通过");
}
private void 李四审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("李四不通过");
// 调用宿主的静态方法终止工作流
Program.Stop();
}
private void 王五审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("王五通过");
}
}
{
public Workflow1()
{
InitializeComponent();
}
private void 张三审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("张三通过");
}
private void 李四审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("李四不通过");
// 调用宿主的静态方法终止工作流
Program.Stop();
}
private void 王五审核_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("王五通过");
}
}
宿主类:
class Program
{
static WorkflowRuntime workflowRuntime;
static WorkflowInstance instance;
static AutoResetEvent waitHandle;
static void Main(string[] args)
{
workflowRuntime = new WorkflowRuntime();
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();
};
instance = workflowRuntime.CreateWorkflow(typeof(BreakWorkflow.Workflow1));
instance.Start();
waitHandle.WaitOne();
}
/// <summary>
/// 终止工作流的实例
/// </summary>
public static void Stop()
{
// 将阻止工作流的线程继续,否则在执行Terminate()方法时会产生异常
waitHandle.Set();// 如果不执行该语句,在执行Terminate()方法时会产生异常
instance.Terminate("终止信息");
}
}
{
static WorkflowRuntime workflowRuntime;
static WorkflowInstance instance;
static AutoResetEvent waitHandle;
static void Main(string[] args)
{
workflowRuntime = new WorkflowRuntime();
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();
};
instance = workflowRuntime.CreateWorkflow(typeof(BreakWorkflow.Workflow1));
instance.Start();
waitHandle.WaitOne();
}
/// <summary>
/// 终止工作流的实例
/// </summary>
public static void Stop()
{
// 将阻止工作流的线程继续,否则在执行Terminate()方法时会产生异常
waitHandle.Set();// 如果不执行该语句,在执行Terminate()方法时会产生异常
instance.Terminate("终止信息");
}
}
执行结果如下: