DDD工作流持久化(十六)

2018/02/25 18:13
阅读数 57

找到对应的sql文件执行sql语句

产生如下的表:

添加引用:

添加命名空间:

using System.Activities.DurableInstancing;
using System.Runtime.DurableInstancing;

 对持久化的存储进行配置:

WorkflowApplication instance;
        SqlWorkflowInstanceStore instancestore;

        private void InstanceConfig()
        {
            instance = new WorkflowApplication(new ApprovalWorkflow.ApprovalWorkflow());
            instancestore = new SqlWorkflowInstanceStore();
            instancestore.ConnectionString = ConfigurationManager.AppSettings["constr"];
            instance.InstanceStore = instancestore;
            instance.Idle =
                new Action<WorkflowApplicationIdleEventArgs>(workflowidle);
            instance.PersistableIdle =
                new Func<WorkflowApplicationIdleEventArgs, PersistableIdleAction>(workflowpersist);
            instance.Completed =
                new Action<WorkflowApplicationCompletedEventArgs>(workflowcompleted);
        }
private PersistableIdleAction workflowpersist(WorkflowApplicationIdleEventArgs args)
        {
            return PersistableIdleAction.Unload;
        }

 执行查询语句:

SqlConnection conn = null;
        SqlCommand cmd = null;
        private void ResumeActivity(string bookmarkname,string cb)
        {
            conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.AppSettings["constr"];
            conn.Open();
            cmd =
                new SqlCommand("select Id from [System.Activities.DurableInstancing].[InstancesTable]", conn);
            var instanceid=(Guid)cmd.ExecuteScalar();
            InstanceConfig();
            instance.Load(instanceid);
            if (instance != null)
            {
                if(instance.GetBookmarks().Count(p=>p.BookmarkName==bookmarkname)==1)
                instance.ResumeBookmark(bookmarkname, cb);
            }
        }

 WF的跟踪:

定义一个跟踪的参与者类:

引入命名空间:

using System.Activities.Tracking;

 

public class WFTrackParticipent:TrackingParticipant
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record">跟踪的记录</param>
        /// <param name="timeout"></param>
        protected override void Track(TrackingRecord record,TimeSpan timeout)
        {
            WorkflowInstanceRecord workflowinstancerecord = record
                as WorkflowInstanceRecord;
            if(workflowinstancerecord !=null)
            {
                Console.WriteLine("工作流实例ID:{0},工作流状态:{1}", workflowinstancerecord.InstanceId,
                    workflowinstancerecord.State);
            }

            ActivityStateRecord activitystaterecord =
                record as ActivityStateRecord;
            if(activitystaterecord !=null)
            {
                string vars = "";
                foreach(var var in activitystaterecord.Variables)
                {
                    vars = vars + "变量名:" + var.Key + " 变量值:" + var.Value;
                }
                Console.WriteLine("节点名:{0},节点状态:{1},变量信息:{2}",
                    activitystaterecord.Activity.Name, activitystaterecord.State, vars);
            }

        }
    }

 定义筛选的类:

public class WFTracking
    {
        public static WFTrackParticipent GetTrackingInfo()
        {
            TrackingProfile trackingprofiler =
                new TrackingProfile();
            trackingprofiler.Name = "wf tracking profiler";

            WorkflowInstanceQuery workflowinstancequery =
                new WorkflowInstanceQuery();
            workflowinstancequery.States.Add(WorkflowInstanceStates.Started);//开始的状态
            workflowinstancequery.States.Add(WorkflowInstanceStates.Completed);//完成的状态
            workflowinstancequery.States.Add(WorkflowInstanceStates.Persisted);
            workflowinstancequery.States.Add(WorkflowInstanceStates.Resumed);

            ActivityStateQuery activitystatequery =
                new ActivityStateQuery();

            activitystatequery.ActivityName = "*";
            activitystatequery.Arguments.Add("*");
            activitystatequery.Variables.Add("*");
            activitystatequery.States.Add(ActivityStates.Executing);
            activitystatequery.States.Add(ActivityStates.Closed);

            trackingprofiler.Queries.Add(workflowinstancequery);
            trackingprofiler.Queries.Add(activitystatequery);

            WFTrackParticipent wftrackparticipent =
                new WFTrackParticipent();
            wftrackparticipent.TrackingProfile = trackingprofiler;
            return wftrackparticipent;
        }
    }

 启动程序监听的代码:

instance.Extensions.Add(WFTracking.GetTrackingInfo());

 异步线程执行代码:

public class Compute
    {
  AsyncCallback callback;
        IAsyncResult asyncResult;
        int n;
        int result;
        public IAsyncResult BeginmyCall(int n, AsyncCallback callback, object asyncState)
        {           
            this.n = n;
            this.callback = callback;
            this.asyncResult = new myAsyncResult() { AsyncState = asyncState };
           Thread thread = new Thread(new ThreadStart(myProcessThread));
            thread.Start();
            return asyncResult;
        }

        public void EndmyCall(IAsyncResult r)
        {
            
        }

        public void myProcessThread()
        {            
            for (int i = 1; i <= n; i++)
            {
                Thread.Sleep(1000);
                result = result + i;
                Console.WriteLine(i);                
            }
            Console.WriteLine(result.ToString());
            this.callback(this.asyncResult);
        }
    }

 

public class myAsyncResult : IAsyncResult
    {
        public object AsyncState
        {
            get; set;
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get; set;
        }

        public bool CompletedSynchronously
        {
            get
            {
                return true;
            }
        }

        public bool IsCompleted
        {
            get
            {
                return true;
            }
        }
    }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部