工作流学习之Activity类
1 Activity的结构说明
2 CompositeActivity与Activity
1. CompositeActivity继承Activity
2. CompositeActivity有Activities集合, Activity没有Activities集合
3. 要CompositeActivity向添加Activity时,要将CanModifyActivities属性(保护级)设为真,添加完成后,要将该属性设为假
3 Activity类
System.Workflow.ComponentModel.Activity
3.1 构造函数
重载1 |
new System.Workflow.ComponentModel.Activity(); |
重载2 |
new System.Workflow.ComponentModel.Activity("结点对象的名子"); |
参数信会赋值到Activity的Name属性 如果不传该参,默认的Name属性将为"Activity" |
3.2 公共成员
3.2.1 属性
Name |
Activity的属性,可以由构选函数赋值传入 在同一容器中不能有相同Name的Activity |
QualifiedName |
与Name属性相同 |
Description |
Activity描述信息 |
Enabled |
Activity是否启用,如果Activity不启用,将不会执行该Activity |
Parent |
返回Activity对象 该Activity对象的容器对象 只读 |
ExecutionStatus |
该Activity的执行状态 只读 类型为ActivityExecutionStatus枚举 |
public enum ActivityExecutionStatus { Initialized = 0, Executing = 1, Canceling = 2, Closed = 3, Compensating = 4, Faulting = 5, } |
|
ExecutionResult |
该Activity的执行结果 只读 类型为ActivityExecutionResult枚举 |
public enum ActivityExecutionResult { None = 0, Succeeded = 1, Canceled = 2, Compensated = 3, Faulted = 4, Uninitialized = 5, } |
3.2.2 方法
Load (静态) |
Overloaded. Loads an instance of an Activity from a Stream. |
Save |
Overloaded. Saves a copy of the Activity to a Stream. |
GetActivityByName |
Overloaded. Returns the instance of the Activity whose name is requested from the set of all activities running under the root activity of this instance; that is within the workflow. |
RaiseEvent |
Raises an Event associated with the specified dependency property. |
RaiseGenericEvent |
Raises the event associated with the referenced DependencyProperty. |
RegisterForStatusChange |
Registers the specified DependencyProperty for the status change event. |
UnregisterForStatusChange |
Un-registers the specified DependencyProperty for the status change event. |
3.2.3 事件
Canceling |
Occurs when the activity execution is cancelled. |
Closed |
Occurs when an Activity has completed execution. |
Compensating |
Activity完成时 |
Executing |
Activity执行时 |
Faulting |
Occurs when an exception is raised during the running of the instance. |
StatusChanged |
Activity状态改变时 |
3.3 保护成员
3.3.1 属性
WorkflowInstanceId |
Activity的工作流实例ID 只读, 得到工作流的ID |
|
|
|
|
3.3.2 方法
Cancel |
Method used to cancel execution of an activity. |
Execute |
Runs the activity synchronously. |
GetDynamicActivities |
Provides an array of activity instances which are dynamically created at run-time |
|
|
HandleFault |
Called when an exception is raised within the context of the execution of this instance. |
Initialize |
When overridden in a derived class, provides initialization by a service provider for the activity. |
Invoke |
Overloaded. |
OnClose |
Runs when the activity is closed. |
TrackData |
Overloaded. Informs the run-time tracking infrastructure of pending tracking information. |
4 CompositeActivity类
System.Workflow.ComponentModel.CompositeActivity
CompositeActivity继承Activity,
可以成为Activity的容器
4.1 公共成员
4.1.1 属性
Activities |
子Activity集合 向Activities 集合中添加Activity要先将CanModifyActivities属性设为真,添加完成后,要将CanModifyActivities属性设为假 或者使用WorkflowChanges方式添加 |
EnabledActivities |
Gets the Read-Only Collection which represents the subset of Activities which are enabled. |
4.2 保护成员
4.2.1 属性
CanModifyActivities |
Gets or sets a value which controls whether the individual activities within Activities can be modified. |
4.2.2 方法
ApplyWorkflowChanges |
Applies the WorkflowChanges manifest in the parameter to this instance. |
Initialize |
Overridden. Initializes all appropriate child activities using the WorkflowCoreRuntime of this instance and the specified IServiceProvider. |
OnActivityChangeAdd |
Calls the specified ActivityExecutionContextInitializeinitialize method using the specified Activity. |
OnActivityChangeRemove |
Called when an activity is removed. |
OnListChanged |
Performs additional processing when the Activities changes. |
OnListChanging |
Event which occurs before a change being made to the underlying Activities. |
OnWorkflowChangesCompleted |
Called after changes have been made to the collection Activities of this instance. |
5 递归列出工作流结点
5.1 打印结点
public static void ShowActivityStruct(object activity) { if (activity is System.Workflow.ComponentModel.CompositeActivity) { System.Workflow.ComponentModel.CompositeActivity wxd; wxd = (System.Workflow.ComponentModel.CompositeActivity)activity; string s; s = string.Format("说明:{0},名称:{1},类型:{2}", wxd.Description, wxd.QualifiedName, wxd.GetType().ToString()); Console.WriteLine(s); foreach (object temp in wxd.Activities) { ShowActivityStruct(temp); } } else { if (activity is System.Workflow.ComponentModel.Activity) { System.Workflow.ComponentModel.Activity wxd; wxd = (System.Workflow.ComponentModel.Activity)activity; string s; s = string.Format("说明:{0},名称:{1},类型:{2}", wxd.Description, wxd.QualifiedName, wxd.GetType().ToString()); Console.WriteLine(s); } } } |
static void { WorkflowRuntime workflowRuntime = new WorkflowRuntime(); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication2.Workflow1)); ShowActivityStruct(instance.GetWorkflowDefinition()); } |
|
5.2 显示到树
public void 列结点(object obj ,TreeNode v) { if (obj is System.Workflow.ComponentModel.CompositeActivity) { System.Workflow.ComponentModel.CompositeActivity wxd; wxd = (System.Workflow.ComponentModel.CompositeActivity)obj; string s; s = string.Format("{1}[{2}]({0})", wxd.Description, wxd.QualifiedName, wxd.GetType().ToString()); System.Windows.Forms.TreeNode n = new TreeNode(); n.Text = s; v.Nodes.Add(n); foreach (object temp in wxd.Activities) { 列结点(temp,n); } } else { if (obj is System.Workflow.ComponentModel.Activity) { System.Workflow.ComponentModel.Activity wxd; wxd = (System.Workflow.ComponentModel.Activity)obj; string s; s = string.Format("{1}[{2}]({0})", wxd.Description, wxd.QualifiedName, wxd.GetType().ToString()); System.Windows.Forms.TreeNode n = new TreeNode(); n.Text = s; v.Nodes.Add(n); } } } } |
private void btShowWorkflowView_Click(object sender, EventArgs e) { this.treeView1.Nodes[0].Nodes.Clear(); if (this.listBox3.SelectedItem != null) { WorkflowInstance temp; System.Guid gid = new Guid(this.listBox3.SelectedItem.ToString()); temp = WFEngine.GetWorkflow(gid); this.treeView1.Nodes[0].Text = gid.ToString(); 列结点(temp.GetWorkflowDefinition(),this.treeView1.Nodes[0]); this.treeView1.ExpandAll(); }
|
6 得到工作流结点的执行状态信息
注意以下方法只能在工作流内部调用,或者由工作流内部将该信息抛出
使用WorkflowInstance.GetWorkflowDefinition()方法得到的Activity对象是实例的复本对象,因此
ShowActivityExecutionInfo(WorkflowInstance.GetWorkflowDefinition())得到的并不是工作流的真实数据
public void ShowActivityExecutionInfo(object activity) { if (activity is System.Workflow.ComponentModel.CompositeActivity) { System.Workflow.ComponentModel.CompositeActivity wxd; wxd = (System.Workflow.ComponentModel.CompositeActivity)activity; string s; s = string.Format("名称:{0},结果:{1},执行状态:{2}", wxd.Name, wxd.ExecutionResult.ToString(), wxd.ExecutionStatus.ToString()); Console.WriteLine(s); foreach (object temp in wxd.Activities) { ShowActivityExecutionInfo(temp); } } else { if (activity is System.Workflow.ComponentModel.Activity) { System.Workflow.ComponentModel.Activity wxd; wxd = (System.Workflow.ComponentModel.Activity)activity; string s; s = string.Format("名称:{0},结果:{1},执行状态:{2}", wxd.Name, wxd.ExecutionResult.ToString(), wxd.ExecutionStatus.ToString()); Console.WriteLine(s); } } } |