跟踪workflow instance 状态

news/2024/9/20 12:15:48
场景是这样的:
1.workflowruntime启动了持久化和监听服务
2.workfllowruntime创建多个实例,并启动,一些会长时间延时,一些会中途暂停,会不同的执行状态(业务状态)
3.另有一winform控制台,有个表格,刷新显示每个实例的信息,包括业务状态--比如创建,运行,挂起等
4.通过workflowruntime.GetLoadedWorkflows()方法取得所有实例,但却没有办法得到正确的业务状态

当然,当将实例unload,再load后(实例回写到数据库),通过SqlTrackingQuery查询得到SqlTrackingWorkflowInstance.Status是可以.可是实际上,不可能一刷新实例表格就要将实例unload.所以,在想有没有办法在workflowruntime平台上,获取实例的业务状态. 
决定自己写个类,解决这个问题,思路如下:
1.在宿主中构建Dictionary<Guid, TrackingStatus>,用于维护instance状态
2.预订workflowruntime与instance相关的事件
3.在事件处理方法中更新instance对应的trackingstatus
4.定义event WorkflowStatusChangeEventHandler WorkflowStatusChanged事件,以便状态变化时处理

两个类:
ContractedBlock.gifExpandedBlockStart.gifTrackingStatus
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifusing System.Workflow;
 6None.gifusing System.Workflow.Runtime;
 7None.gifusing System.Workflow.Runtime.Hosting;
 8None.gifusing System.Workflow.Runtime.Tracking;
 9None.gifusing System.Workflow.Activities;
10None.gifusing System.Workflow.ComponentModel;
11None.gif
12None.gifnamespace WFDebugger
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14InBlock.gif    public class TrackingStatus
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        private WorkflowInstance instance;
17InBlock.gif        private WorkflowStatus status;
18InBlock.gif        private DateTime lasteventtime;
19InBlock.gif
20InBlock.gif        public TrackingStatus(WorkflowInstance instance, WorkflowStatus status, System.DateTime eventtime)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            if (instance == null || eventtime == null)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                throw(new ArgumentNullException("instance|status|eventtime"));
25ExpandedSubBlockEnd.gif            }

26InBlock.gif            this.instance = instance;
27InBlock.gif            this.status = status;
28InBlock.gif            this.lasteventtime = eventtime;
29ExpandedSubBlockEnd.gif        }

30InBlock.gif
31InBlock.gif        public TrackingStatus(WorkflowInstance instance, WorkflowStatus status)
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            if (instance == null)
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
35InBlock.gif                throw (new ArgumentNullException("instance|status|eventtime"));
36ExpandedSubBlockEnd.gif            }

37InBlock.gif            this.instance = instance;
38InBlock.gif            this.status = status;
39InBlock.gif            this.lasteventtime = DateTime.Now;
40ExpandedSubBlockEnd.gif        }

41InBlock.gif    
42InBlock.gif        public WorkflowInstance Instance
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            get
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                return instance;
47ExpandedSubBlockEnd.gif            }

48ExpandedSubBlockEnd.gif        }

49InBlock.gif
50InBlock.gif        public WorkflowStatus Status
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            get
53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
54InBlock.gif                return status;
55ExpandedSubBlockEnd.gif            }

56ExpandedSubBlockEnd.gif        }

57InBlock.gif
58InBlock.gif        public DateTime LastEventTime
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif            get
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                return lasteventtime;
63ExpandedSubBlockEnd.gif            }

64ExpandedSubBlockEnd.gif        }

65InBlock.gif
66InBlock.gif        public void ChangeStatus(WorkflowStatus status, DateTime eventtime)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
68InBlock.gif            if (!TryChangeStatus(status, eventtime,new TimeSpan(0,0,10)))
69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
70InBlock.gif                throw(new Exception("can't lock variable"));
71ExpandedSubBlockEnd.gif            }

72ExpandedSubBlockEnd.gif        }

73InBlock.gif
74InBlock.gif        public void ChangeStatus(WorkflowStatus status)
75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
76InBlock.gif            ChangeStatus(status, DateTime.Now);
77ExpandedSubBlockEnd.gif        }

78InBlock.gif
79InBlock.gif        public bool TryChangeStatus(WorkflowStatus status, DateTime eventtime,TimeSpan timeout)
80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
81InBlock.gif            if (System.Threading.Monitor.TryEnter(this.status, timeout))
82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
83InBlock.gif                this.status = status;
84InBlock.gif                lasteventtime = eventtime;
85InBlock.gif                return true;
86ExpandedSubBlockEnd.gif            }

87InBlock.gif            else
88ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
89InBlock.gif                return false;
90ExpandedSubBlockEnd.gif            }

91ExpandedSubBlockEnd.gif        }

92InBlock.gif        public bool TryChangeStatus(WorkflowStatus status, TimeSpan timeout)
93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
94InBlock.gif            return TryChangeStatus(status,DateTime.Now, timeout);
95ExpandedSubBlockEnd.gif        }

96ExpandedSubBlockEnd.gif    }

97ExpandedBlockEnd.gif}

98None.gif
99None.gif

ContractedBlock.gifExpandedBlockStart.gifTrackingWorkflowEvent
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Collections.Generic;
  4None.gifusing System.Data;
  5None.gif
  6None.gifusing System.Workflow;
  7None.gifusing System.Workflow.Runtime;
  8None.gifusing System.Workflow.Runtime.Hosting;
  9None.gifusing System.Workflow.Runtime.Tracking;
 10None.gifusing System.Workflow.Activities;
 11None.gifusing System.Workflow.ComponentModel;
 12None.gifusing System.Collections.ObjectModel;
 13None.gif
 14None.gif
 15None.gif
 16None.gifnamespace WFDebugger
 17ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 18InBlock.gif    class TrackingWorkflowEvent:IDisposable     
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 20InBlock.gif
 21ContractedSubBlock.gifExpandedSubBlockStart.gif        事件#region 事件
 22InBlock.gif        public delegate void WorkflowStatusChangeEventHandler(object sender, WorkflowStatusChangeEventArgs e);
 23InBlock.gif        public event WorkflowStatusChangeEventHandler WorkflowStatusChanged;
 24InBlock.gif        void RaiseWorkflowStatusChangeEvent(WorkflowRuntime runtime,WorkflowStatusChangeEventArgs e)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            if (WorkflowStatusChanged != null)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 28InBlock.gif                WorkflowStatusChanged.Invoke(runtime, e);
 29ExpandedSubBlockEnd.gif            }

 30ExpandedSubBlockEnd.gif        }

 31ExpandedSubBlockEnd.gif        #endregion

 32InBlock.gif
 33ContractedSubBlock.gifExpandedSubBlockStart.gif        私有变量#region 私有变量
 34InBlock.gif        private WorkflowRuntime workflowruntime;
 35InBlock.gif        private SqlTrackingService trackingservice;
 36InBlock.gif        private SqlWorkflowPersistenceService persistenceService;
 37InBlock.gif        private bool initialized = false;
 38InBlock.gif        private bool tacked = false;
 39InBlock.gif
 40InBlock.gif        private Dictionary<Guid, TrackingStatus> dic = new Dictionary<Guid, TrackingStatus>();
 41InBlock.gif
 42ExpandedSubBlockEnd.gif        #endregion

 43InBlock.gif
 44InBlock.gif        public TrackingWorkflowEvent(WorkflowRuntime workflowruntime)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            if (workflowruntime != null)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                this.workflowruntime = workflowruntime;
 49InBlock.gif                ReadOnlyCollection<SqlTrackingService> tcol = workflowruntime.GetAllServices<SqlTrackingService>();
 50InBlock.gif                if (tcol != null && tcol.Count > 0)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 52InBlock.gif                    trackingservice = tcol[0];
 53ExpandedSubBlockEnd.gif                }

 54InBlock.gif                else
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 56InBlock.gif                    throw (new Exception("Workflowruntime havn't TrackingService."));
 57ExpandedSubBlockEnd.gif                }

 58InBlock.gif
 59InBlock.gif                ReadOnlyCollection<SqlWorkflowPersistenceService> pcol = workflowruntime.GetAllServices<SqlWorkflowPersistenceService>();
 60InBlock.gif                if (pcol != null && pcol.Count > 0)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 62InBlock.gif                    persistenceService = pcol[0];
 63ExpandedSubBlockEnd.gif                }

 64InBlock.gif                else
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 66InBlock.gif                    throw (new Exception("Workflowruntime havn't WorkflowPersistenceService."));
 67ExpandedSubBlockEnd.gif                }

 68InBlock.gif
 69InBlock.gif                //runtime event
 70InBlock.gif                workflowruntimeStarted = new EventHandler<WorkflowRuntimeEventArgs>(workflowRuntime_Started);
 71InBlock.gif                workflowruntimeServicesExceptionNotHandled = new EventHandler<ServicesExceptionNotHandledEventArgs>(workflowRuntime_ServicesExceptionNotHandled);
 72InBlock.gif                workflowruntimeStopped = new EventHandler<WorkflowRuntimeEventArgs>(workflowRuntime_Stopped);
 73InBlock.gif
 74InBlock.gif                //instance event
 75InBlock.gif                workflowruntimeWorkflowCompleted = new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
 76InBlock.gif                workflowruntimeWorkflowTerminated = new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);
 77InBlock.gif                workflowruntimeWorkflowSuspended = new EventHandler<WorkflowSuspendedEventArgs>(workflowRuntime_WorkflowSuspended);
 78InBlock.gif                workflowruntimeWorkflowAborted = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowAborted);
 79InBlock.gif                workflowruntimeWorkflowResumed = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowResumed);
 80InBlock.gif                workflowruntimeWorkflowLoaded = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowLoaded);
 81InBlock.gif                workflowruntimeWorkflowIdled = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
 82InBlock.gif                workflowruntimeWorkflowPersisted = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowPersisted);
 83InBlock.gif                workflowruntimeWorkflowUnloaded = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowUnloaded);
 84InBlock.gif                workflowruntimeWorkflowCreated = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowCreated);
 85InBlock.gif                workflowruntimeWorkflowStarted = new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowStarted);
 86InBlock.gif
 87InBlock.gif                initialized = true;
 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif            else
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 91InBlock.gif                throw (new ArgumentNullException("workflowruntime"));
 92ExpandedSubBlockEnd.gif            }

 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95ContractedSubBlock.gifExpandedSubBlockStart.gif        私有方法#region 私有方法
 96InBlock.gif        private bool AddInstance(WorkflowInstance instance)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            if (instance == null)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                throw(new ArgumentNullException("instance"));
101ExpandedSubBlockEnd.gif            }

102InBlock.gif            if (instance.WorkflowRuntime != workflowruntime)
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                throw (new Exception("different workflowruntime"));
105ExpandedSubBlockEnd.gif            }

106InBlock.gif            SqlTrackingQuery sq = new SqlTrackingQuery(trackingservice.ConnectionString);
107InBlock.gif            SqlTrackingWorkflowInstance sqi ;
108InBlock.gif            if (sq.TryGetWorkflow(instance.InstanceId, out sqi))
109ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
110InBlock.gif                dic.Add(instance.InstanceId, new TrackingStatus(instance, sqi.Status));
111InBlock.gif                RaiseWorkflowStatusChangeEvent(workflowruntime, new WorkflowStatusChangeEventArgs(instance,sqi.Status));
112InBlock.gif                return true;
113ExpandedSubBlockEnd.gif            }

114InBlock.gif            else
115InBlock.gif                return false;
116ExpandedSubBlockEnd.gif        }

117InBlock.gif        private bool AddInstance(WorkflowInstance instance,WorkflowStatus status)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            if (instance == null)
120ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
121InBlock.gif                throw (new ArgumentNullException("instance"));
122ExpandedSubBlockEnd.gif            }

123InBlock.gif            if (instance.WorkflowRuntime != workflowruntime)
124ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
125InBlock.gif                throw (new Exception("different workflowruntime"));
126ExpandedSubBlockEnd.gif            }

127InBlock.gif            dic.Add(instance.InstanceId, new TrackingStatus(instance, status));
128InBlock.gif            RaiseWorkflowStatusChangeEvent(workflowruntime, new WorkflowStatusChangeEventArgs(instance, status));
129InBlock.gif            return true;
130ExpandedSubBlockEnd.gif        }

131InBlock.gif
132InBlock.gif        private void RemoveInstance(WorkflowInstance instance)
133ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
134InBlock.gif            if (instance == null)
135ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
136InBlock.gif                throw (new ArgumentNullException("instance"));
137ExpandedSubBlockEnd.gif            }

138InBlock.gif            if (instance.WorkflowRuntime != workflowruntime)
139ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
140InBlock.gif                throw (new Exception("different workflowruntime"));
141ExpandedSubBlockEnd.gif            }

142InBlock.gif            RemoveInstance(instance.InstanceId);
143ExpandedSubBlockEnd.gif        }

144InBlock.gif        private void RemoveInstance(Guid instanceid)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            //if (instanceid == null)
147InBlock.gif            //{
148InBlock.gif            //    throw (new ArgumentNullException("instanceid"));
149InBlock.gif            //}
150InBlock.gif            if (dic.ContainsKey(instanceid))
151ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
152InBlock.gif                dic.Remove(instanceid);
153ExpandedSubBlockEnd.gif            }

154InBlock.gif            else
155ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
156InBlock.gif                throw (new Exception("no found appointed instance"));
157ExpandedSubBlockEnd.gif            }

158InBlock.gif
159InBlock.gif
160ExpandedSubBlockEnd.gif        }

161InBlock.gif        private bool ChangeStatus(WorkflowInstance instance, WorkflowStatus status)
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            if (instance == null)
164ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
165InBlock.gif                throw (new ArgumentNullException("instance"));
166ExpandedSubBlockEnd.gif            }

167InBlock.gif            //if (status == null)
168InBlock.gif            //{
169InBlock.gif            //    throw (new ArgumentNullException("status"));
170InBlock.gif            //}
171InBlock.gif            if (dic.ContainsKey(instance.InstanceId))
172ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
173InBlock.gif                TrackingStatus ts = dic[instance.InstanceId];
174InBlock.gif                bool r = ts.TryChangeStatus(status,new TimeSpan(0,0,10));
175InBlock.gif                RaiseWorkflowStatusChangeEvent(workflowruntime, new WorkflowStatusChangeEventArgs(instance, status));
176InBlock.gif                return r;
177ExpandedSubBlockEnd.gif            }

178InBlock.gif            else
179ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
180InBlock.gif                return false;
181InBlock.gif                //return AddInstance(instance, status);
182ExpandedSubBlockEnd.gif            }

183InBlock.gif
184InBlock.gif
185ExpandedSubBlockEnd.gif        }

186ExpandedSubBlockEnd.gif        #endregion

187InBlock.gif
188ContractedSubBlock.gifExpandedSubBlockStart.gif        公开属性#region 公开属性
189ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
190InBlock.gif        /// 工作流环境
191ExpandedSubBlockEnd.gif        /// </summary>

192InBlock.gif        public WorkflowRuntime WorkflowRuntime
193ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
194InBlock.gif            get
195ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
196InBlock.gif                return workflowruntime;
197ExpandedSubBlockEnd.gif            }

198ExpandedSubBlockEnd.gif        }

199InBlock.gif
200ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
201InBlock.gif        /// 实例数量
202ExpandedSubBlockEnd.gif        /// </summary>

203InBlock.gif        public int Count
204ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
205InBlock.gif            get
206ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
207InBlock.gif                return dic.Count;
208ExpandedSubBlockEnd.gif            }

209ExpandedSubBlockEnd.gif        }

210InBlock.gif
211ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
212InBlock.gif        /// 是否在跟踪
213ExpandedSubBlockEnd.gif        /// </summary>

214InBlock.gif        public bool IsOnTracking
215ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
216InBlock.gif            get
217ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
218InBlock.gif                return tacked;
219ExpandedSubBlockEnd.gif            }

220ExpandedSubBlockEnd.gif        }

221ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
222InBlock.gif        /// 是否初始化
223ExpandedSubBlockEnd.gif        /// </summary>

224InBlock.gif        public bool Initialized
225ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
226InBlock.gif            get
227ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
228InBlock.gif                return initialized;
229ExpandedSubBlockEnd.gif            }

230ExpandedSubBlockEnd.gif        }

231InBlock.gif        public WorkflowStatus GetWorkflowStatus(Guid instanceID)
232ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
233InBlock.gif            //if (instanceID == null)
234InBlock.gif            //{
235InBlock.gif            //    throw(new ArgumentNullException("instanceID"));
236InBlock.gif            //}
237InBlock.gif            if (dic.ContainsKey(instanceID))
238ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
239InBlock.gif                return dic[instanceID].Status;
240ExpandedSubBlockEnd.gif            }

241InBlock.gif            else
242ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
243InBlock.gif                throw(new Exception("no found appointed instance"));
244InBlock.gif                //return AddInstance(instance, status);
245ExpandedSubBlockEnd.gif            }

246ExpandedSubBlockEnd.gif        }

247InBlock.gif        public WorkflowStatus GetWorkflowStatus(WorkflowInstance instance)
248ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
249InBlock.gif            if (instance == null)
250ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
251InBlock.gif                throw (new ArgumentNullException("instance"));
252ExpandedSubBlockEnd.gif            }

253InBlock.gif            if (instance.WorkflowRuntime != workflowruntime)
254ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
255InBlock.gif                throw (new Exception("different workflowruntime"));
256ExpandedSubBlockEnd.gif            }

257InBlock.gif            return GetWorkflowStatus(instance.InstanceId);
258ExpandedSubBlockEnd.gif        }

259InBlock.gif
260ExpandedSubBlockEnd.gif        #endregion
 
261InBlock.gif
262ContractedSubBlock.gifExpandedSubBlockStart.gif        Runtime Event#region Runtime Event
263InBlock.gif        
264ContractedSubBlock.gifExpandedSubBlockStart.gif        事件句柄#region 事件句柄
265InBlock.gif        //runtime event
266InBlock.gif        EventHandler<WorkflowRuntimeEventArgs>              workflowruntimeStarted;
267InBlock.gif        EventHandler<ServicesExceptionNotHandledEventArgs>  workflowruntimeServicesExceptionNotHandled;
268InBlock.gif        EventHandler<WorkflowRuntimeEventArgs>              workflowruntimeStopped;
269InBlock.gif
270InBlock.gif        //instance event
271InBlock.gif        EventHandler<WorkflowCompletedEventArgs>            workflowruntimeWorkflowCompleted;
272InBlock.gif        EventHandler<WorkflowTerminatedEventArgs>           workflowruntimeWorkflowTerminated;
273InBlock.gif        EventHandler<WorkflowSuspendedEventArgs>            workflowruntimeWorkflowSuspended;
274InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowAborted;
275InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowResumed;
276InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowLoaded;
277InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowIdled;
278InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowPersisted;
279InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowUnloaded;
280InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowCreated;
281InBlock.gif        EventHandler<WorkflowEventArgs>                     workflowruntimeWorkflowStarted;
282InBlock.gif
283InBlock.gif        public void TackEvent()
284ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
285InBlock.gif            if (initialized && !tacked)
286ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
287ContractedSubBlock.gifExpandedSubBlockStart.gif                预定事件#region 预定事件
288InBlock.gif                //runtime event
289InBlock.gif                workflowruntime.Started += workflowruntimeStarted;
290InBlock.gif                workflowruntime.ServicesExceptionNotHandled += workflowruntimeServicesExceptionNotHandled;
291InBlock.gif                workflowruntime.Stopped += workflowruntimeStopped;
292InBlock.gif
293InBlock.gif                //instance event
294InBlock.gif                workflowruntime.WorkflowCompleted += workflowruntimeWorkflowCompleted;
295InBlock.gif                workflowruntime.WorkflowTerminated += workflowruntimeWorkflowTerminated;
296InBlock.gif                workflowruntime.WorkflowSuspended += workflowruntimeWorkflowSuspended;
297InBlock.gif                workflowruntime.WorkflowAborted += workflowruntimeWorkflowAborted;
298InBlock.gif                workflowruntime.WorkflowResumed += workflowruntimeWorkflowResumed;
299InBlock.gif                workflowruntime.WorkflowLoaded += workflowruntimeWorkflowLoaded;
300InBlock.gif                workflowruntime.WorkflowIdled += workflowruntimeWorkflowIdled;
301InBlock.gif                workflowruntime.WorkflowPersisted += workflowruntimeWorkflowPersisted;
302InBlock.gif                workflowruntime.WorkflowUnloaded += workflowruntimeWorkflowUnloaded;
303InBlock.gif                workflowruntime.WorkflowCreated += workflowruntimeWorkflowCreated;
304InBlock.gif                workflowruntime.WorkflowStarted += workflowruntimeWorkflowStarted;
305ExpandedSubBlockEnd.gif                #endregion

306InBlock.gif                tacked = true;
307ExpandedSubBlockEnd.gif            }

308InBlock.gif            else
309ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
310InBlock.gif                thrownew Exception("TrackingWorkflowEvent didn't initialized or TrackingInstanceEvent tacked event"));
311ExpandedSubBlockEnd.gif            }

312ExpandedSubBlockEnd.gif        }

313InBlock.gif
314InBlock.gif        public void UntackEvent()
315ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
316InBlock.gif            if (initialized && tacked)
317ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
318ContractedSubBlock.gifExpandedSubBlockStart.gif                卸载事件#region 卸载事件
319InBlock.gif                //runtime event
320InBlock.gif                workflowruntime.Started -= workflowruntimeStarted;
321InBlock.gif                workflowruntime.ServicesExceptionNotHandled -= workflowruntimeServicesExceptionNotHandled;
322InBlock.gif                workflowruntime.Stopped -= workflowruntimeStopped;
323InBlock.gif
324InBlock.gif                //instance event
325InBlock.gif                workflowruntime.WorkflowCompleted -= workflowruntimeWorkflowCompleted;
326InBlock.gif                workflowruntime.WorkflowTerminated -= workflowruntimeWorkflowTerminated;
327InBlock.gif                workflowruntime.WorkflowSuspended -= workflowruntimeWorkflowSuspended;
328InBlock.gif                workflowruntime.WorkflowAborted -= workflowruntimeWorkflowAborted;
329InBlock.gif                workflowruntime.WorkflowResumed -= workflowruntimeWorkflowResumed;
330InBlock.gif                workflowruntime.WorkflowLoaded -= workflowruntimeWorkflowLoaded;
331InBlock.gif                workflowruntime.WorkflowIdled -= workflowruntimeWorkflowIdled;
332InBlock.gif                workflowruntime.WorkflowPersisted -= workflowruntimeWorkflowPersisted;
333InBlock.gif                workflowruntime.WorkflowUnloaded -= workflowruntimeWorkflowUnloaded;
334InBlock.gif                workflowruntime.WorkflowCreated -= workflowruntimeWorkflowCreated;
335InBlock.gif                workflowruntime.WorkflowStarted -= workflowruntimeWorkflowStarted;
336InBlock.gif
337ExpandedSubBlockEnd.gif                #endregion

338InBlock.gif
339InBlock.gif                tacked = false;
340ExpandedSubBlockEnd.gif            }

341InBlock.gif            else
342ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
343InBlock.gif                throw (new Exception("TrackingWorkflowEvent didn't initialized or TrackingInstanceEvent didn't tack event"));
344ExpandedSubBlockEnd.gif            }

345ExpandedSubBlockEnd.gif        }

346InBlock.gif        
347ExpandedSubBlockEnd.gif        #endregion

348InBlock.gif
349ContractedSubBlock.gifExpandedSubBlockStart.gif        事件处理#region 事件处理
350InBlock.gif
351ContractedSubBlock.gifExpandedSubBlockStart.gif        runtime event#region runtime event
352InBlock.gif        void workflowRuntime_Started(object sender, WorkflowRuntimeEventArgs e)
353ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
354InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow runtime started(启动)\tDateTime(时间) : {1}.{2}", e.IsStarted, DateTime.Now, DateTime.Now.Millisecond);
355ExpandedSubBlockEnd.gif        }

356InBlock.gif
357InBlock.gif        void workflowRuntime_ServicesExceptionNotHandled(object sender, ServicesExceptionNotHandledEventArgs e)
358ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
359InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow runtime services exception not handled(异常)[{0}]\tDateTime(时间) : {1}.{2}", e.Exception, DateTime.Now, DateTime.Now.Millisecond);
360ExpandedSubBlockEnd.gif        }

361InBlock.gif
362InBlock.gif        void workflowRuntime_Stopped(object sender, WorkflowRuntimeEventArgs e)
363ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
364InBlock.gif            dic.Clear();
365InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow runtime stopped(停止)\tDateTime(时间) : {1}.{2}", e.IsStarted, DateTime.Now, DateTime.Now.Millisecond);
366ExpandedSubBlockEnd.gif        }

367ExpandedSubBlockEnd.gif        #endregion
 runtime event
368InBlock.gif
369ContractedSubBlock.gifExpandedSubBlockStart.gif        instance event#region instance event
370InBlock.gif        void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
371ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
372InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Completed);
373InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tcompleted(完成)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
374ExpandedSubBlockEnd.gif        }

375InBlock.gif
376InBlock.gif        void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
377ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
378InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Terminated);
379InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tterminated(结束)\tDateTime(时间) : {1}.{2}\tmessage:{3}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond, e.Exception.Message);
380ExpandedSubBlockEnd.gif        }

381InBlock.gif
382InBlock.gif        void workflowRuntime_WorkflowSuspended(object sender, WorkflowSuspendedEventArgs e)
383ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
384InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Suspended);
385InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tsuspended(暂停)\tDateTime(时间) : {1}.{2}\tmessage:{3}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond, e.Error);
386ExpandedSubBlockEnd.gif        }

387InBlock.gif
388InBlock.gif        void workflowRuntime_WorkflowResumed(object sender, WorkflowEventArgs e)
389ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
390InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Running);
391InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tresumed(继续)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
392ExpandedSubBlockEnd.gif        }

393InBlock.gif
394InBlock.gif        void workflowRuntime_WorkflowAborted(object sender, WorkflowEventArgs e)
395ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
396InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Terminated);
397InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \taborted(中断)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
398ExpandedSubBlockEnd.gif        }

399InBlock.gif
400InBlock.gif        void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
401ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
402InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Running);
403InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tidled(空闲)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
404ExpandedSubBlockEnd.gif        }

405InBlock.gif
406InBlock.gif        void workflowRuntime_WorkflowUnloaded(object sender, WorkflowEventArgs e)
407ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
408InBlock.gif            RemoveInstance(e.WorkflowInstance);
409InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tunloaded(卸载)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
410ExpandedSubBlockEnd.gif        }

411InBlock.gif
412InBlock.gif        void workflowRuntime_WorkflowPersisted(object sender, WorkflowEventArgs e)
413ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
414InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tpersisted(持久)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
415ExpandedSubBlockEnd.gif        }

416InBlock.gif
417InBlock.gif        void workflowRuntime_WorkflowLoaded(object sender, WorkflowEventArgs e)
418ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
419InBlock.gif            AddInstance(e.WorkflowInstance);
420InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tloaded(加载)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
421ExpandedSubBlockEnd.gif        }

422InBlock.gif
423InBlock.gif        void workflowRuntime_WorkflowCreated(object sender, WorkflowEventArgs e)
424ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
425InBlock.gif            AddInstance(e.WorkflowInstance, WorkflowStatus.Created);
426InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tcreated(创建)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
427ExpandedSubBlockEnd.gif        }

428InBlock.gif
429InBlock.gif        void workflowRuntime_WorkflowStarted(object sender, WorkflowEventArgs e)
430ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
431InBlock.gif            ChangeStatus(e.WorkflowInstance, WorkflowStatus.Running);
432InBlock.gif            Console.WriteLine("TrackingWorkflowEvent: Workflow {0} \tstarted(启动)\tDateTime(时间) : {1}.{2}", e.WorkflowInstance.InstanceId, DateTime.Now, DateTime.Now.Millisecond);
433ExpandedSubBlockEnd.gif        }

434InBlock.gif
435InBlock.gif
436ExpandedSubBlockEnd.gif        #endregion
 instance event
437InBlock.gif
438InBlock.gif
439ExpandedSubBlockEnd.gif        #endregion
 事件处理
440InBlock.gif
441ExpandedSubBlockEnd.gif        #endregion

442InBlock.gif
443ContractedSubBlock.gifExpandedSubBlockStart.gif        IDisposable Members#region IDisposable Members
444InBlock.gif
445InBlock.gif        public void Dispose()
446ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
447InBlock.gif            if (dic != null)
448ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
449InBlock.gif                dic.Clear();
450InBlock.gif                dic = null;
451ExpandedSubBlockEnd.gif            }

452ExpandedSubBlockEnd.gif        }

453InBlock.gif
454ExpandedSubBlockEnd.gif        #endregion

455ExpandedSubBlockEnd.gif    }

456InBlock.gif
457ExpandedBlockEnd.gif}

使用方便:
            trackingworkflowevent = new TrackingWorkflowEvent(workflowRuntime);     //创建
            trackingworkflowevent.TackEvent();                                                          //预订事件
            trackingworkflowevent.GetWorkflowStatus(instance);                                       //获取instance状态
            trackingworkflowevent.UntackEvent();                                                     //卸载事件
            trackingworkflowevent.Dispose();                                                             //消毁


转载于:https://www.cnblogs.com/net66/archive/2006/10/26/541184.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pgtn.cn/news/18611.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

1数字图像获取:1.1图像数字化

图像数字化是数字图像处理的物理技术。 图像数字化原理&#xff1a;图像数字化是将一幅画面转换成计算机能处理的一种形式-------数字图像的过程。 具体来说就是把一幅图像分割成如图的一个个小区域&#xff08;像元或者像素&#xff09;&#xff0c;并将各个小区域的灰度用整…

力扣(LeetCode)刷题,简单+中等题(第36期)

目录 第1题&#xff1a;连续的子数组和 第2题&#xff1a;连续数组 第3题&#xff1a;相交链表 第4题&#xff1a;目标和 第5题&#xff1a;最后一块石头的重量 II 第6题&#xff1a;构造矩形 第7题&#xff1a;零钱兑换 II 第8题&#xff1a;完全平方数 第9题&#x…

LabVIEW图像分割算法(基础篇—6)

目录 1、图像阈值分割 1.1、全局阈值分割 1.1.1、手动阈值分割 1.1.2、自动阈值分割 1.2、局部阈值分割 1.3、阈值分割算法比较 2、图像边缘分割 2.1、点检测 2.2、线检测 2.3、轮廓提取 3、图像形态学分割 3.1、像素的形态学处理 3.2、颗粒的形态学处理 4、图像…

Microsoft Anti-Cross Site Scripting Library V1.5 发布了

Microsoft Anti-Cross Site Scripting Library V1.5 发布了 微软反跨站攻击脚本库 v1.5。此下载包含Microsoft Application Security Anti-Cross Site Scripting Library的分发组件.Anti-Cross Site Scripting Library可以为网站开发人员提供基于Web应用防护,以抵御源自 Cross-…

1数字图像获取:1.2图像灰度直方图

----------1图像灰度直方图的概念------ 灰度直方图是反映一幅图像中各灰度级像素出现的频率与灰度级的关系。以灰度级为横坐标&#xff0c;频率为纵坐标绘制频率同灰度级的关系图就是一副灰度图像的直方图。他是一个图像的重要特征&#xff0c;反映了图像分布灰度的状况。暗图…

和12岁小同志搞创客开发:两个控制器之间如何实现通信?

目录 1、有线通信 2、无线通信 3、串口点灯 机缘巧合在网上认识一位12岁小同志&#xff0c;从零开始系统辅导其创客开发思维和技巧。 ​​​项目专栏&#xff1a;https://blog.csdn.net/m0_38106923/category_11097422.html 本篇博客来讲讲如何实现两个控制器之间数据通信&…

和12岁小同志搞创客开发:设计一款亮度可调节灯

机缘巧合在网上认识一位12岁小同志&#xff0c;从零开始系统辅导其创客开发思维和技巧。 ​​​项目专栏&#xff1a;https://blog.csdn.net/m0_38106923/category_11097422.html 本篇博客来设计一款亮度可调节灯&#xff0c;一起看看吧~ 亮度可调节灯&#xff0c;重点在于可…

1数字图像获取:1.3图像处理算法的形式

图像处理算法就是利用数学原理与计算机程序对数字图像进行处理的基础。 局部处理的例子&#xff1a;对一幅图像采用3x3模板进行卷积运算&#xff0c;用3x3的模板在该图像上进行扫描式的平移&#xff0c;每一个像素的卷积计算值是由并仅由该像素本身和该像素的8邻域像素的计算总…