WF4.5 状态机使用WorkflowApplication 持久化的作用Idle状态与Bookmark(四)

继续上面一篇看持久化的作用与Idle状态

 

获得工作流实例Id用不同的WorkflowApplication进行执行BookMark:

 

添加:

application.Unload()

因为application.Run();的时候是另开线程执行,当执行非常快的时候,有可能还没有CreateBookMark的时候,外面就已经卸载了,所以要加入同步锁,当Idle的时候,set,如下代码:

protected static AutoResetEvent reset = new AutoResetEvent(false);

application.Idle = OnIdle;

application.Run();

reset.WaitOne();

application.Unload();
public static void OnIdle(WorkflowApplicationIdleEventArgs args)
{
      reset.Set();
      Console.WriteLine("OnIdle");
}

以下是读取当前工作流的Id丢给另外一个工作流执行ResumeBookMark的代码:

 class Program
    {
        protected static AutoResetEvent reset = new AutoResetEvent(false);

        static void Main(string[] args)
        {

            //创建活动实例
            StateMachineWorkFlow context = new StateMachineWorkFlow();

            //创建单实例工作流宿主
            WorkflowApplication application = new WorkflowApplication(context);
           
      //添加Idle事件
      application.Idle
= OnIdle; //添加持久化数据库 SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=SampleInstanceStore;Integrated Security=True;Asynchronous Processing=True"); //将持久化数据库添加到工作流宿主主机里面去 application.InstanceStore = instanceStore; //执行 application.Run();
      //当前线程等待 reset.WaitOne();
      
      //获取工作流实列Id Guid installGuid
= application.Id; //卸载当前工作流 application.Unload(); //构建新的application执行BookMark application = new WorkflowApplication(context); application.InstanceStore = instanceStore; application.Load(installGuid); application.ResumeBookmark("FinalWait", null); Console.ReadLine(); } public static void OnIdle(WorkflowApplicationIdleEventArgs args) { reset.Set(); Console.WriteLine("OnIdle"); }

执行效果如下:

WF4.5 状态机使用WorkflowApplication 持久化的作用Idle状态与Bookmark(四)_第1张图片

 

前四章源代码:http://files.cnblogs.com/qugangf/TestWorkFlow.rar

你可能感兴趣的:(application)