通过AEC解读WF的核心原理(十三完)实现Visio设计风格的Activity

通过AEC解读WF的核心原理(十三完)实现Visio设计风格的Activity

 

 

AEC,是ActivityExecutionContext 对像,也是 Activity Execution Context

本系列以ActivityExecutionContext 对像为主线,通过以自定义Activity为例子演示了控制Activity内部执行的一些技巧。如果真正的读懂了本文,就能更好的理解WF工具栏中自带Activity的,并重新实现其功能。

实际上,如果开发业务工作流,直接用WF工具栏中自带Activity拼装会使流程显得很臃肿,而状态机与顺序工作流其实也不过是WF为我们提供的两个简单的Activity执行控制模板而以,WF只的两个Activity,(System.Workflow.ComponentModel.Activity与System.Workflow.ComponentModel.CompositeActivity),它们是活动树上的叶节点与支节点,当你直正理解了这些你会发现让人眼花缭乱的WF背后是如此清爽幽雅,WF仿佛在一个全新的空间再现了计算机工作原理,指令、地址、数据、出栈、入栈.........

 

最后以一个简单的例子结束本系例:

实现Visio设计风格的Activity

先看一下我的一个项目中的效果

 

下面是一个按连接线顺序调用的例子

https://files.cnblogs.com/wxwinter/AEC13.rar

结点Activity

public class 结点: Activity

    {

public Activity 上一结点

{ set; get; }

public Activity 下一结点

{ set; get; }

 

 

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

System.Console.WriteLine(this.Name);

return ActivityExecutionStatus.Closed;

}

    }

 

容器Designer

public class wxwinterDesigner : FreeformActivityDesigner

{

public wxwinterDesigner()

{

this.ConnectorAdded += new ConnectorEventHandler(wxwinterDesigner_ConnectorAdded);

this.ConnectorChanged += new ConnectorEventHandler(wxwinterDesigner_ConnectorChanged);

this.ConnectorRemoved += new ConnectorEventHandler(wxwinterDesigner_ConnectorRemoved);

}

 

void wxwinterDesigner_ConnectorChanged(object sender, ConnectorEventArgs e)

{

结点 s = e.Connector.Source.AssociatedDesigner.Activity as 结点;

结点 t = e.Connector.Target.AssociatedDesigner.Activity as 结点;

 

s.下一结点 = t;

t.上一结点 = s;

 

}

 

void wxwinterDesigner_ConnectorRemoved(object sender, ConnectorEventArgs e)

{

结点 s = e.Connector.Source.AssociatedDesigner.Activity as 结点;

结点 t = e.Connector.Target.AssociatedDesigner.Activity as 结点;

 

t.上一结点 =null ;

s.下一结点 =null;

}

 

void wxwinterDesigner_ConnectorAdded(object sender, ConnectorEventArgs e)

{

结点 s = e.Connector.Source.AssociatedDesigner.Activity as 结点;

结点 t = e.Connector.Target.AssociatedDesigner.Activity as 结点;

s.下一结点 = t;

t.上一结点 = s;

}

}

 

容器Activity

[Designer(typeof(wxwinterDesigner), typeof(IDesigner))]

    public class 容器: SequenceActivity

    {

public 结点 开始结点

{ set; get; }

 

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

if (开始结点 != null)

{

开始结点.Closed += node_Closed;

executionContext.ExecuteActivity(开始结点);

return ActivityExecutionStatus.Executing;

}

else

{

return ActivityExecutionStatus.Closed;

}

}

 

void node_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

ActivityExecutionContext executionContext = sender as ActivityExecutionContext;

结点 node = e.Activity as 结点;

node.Closed -= node_Closed;

 

 

if (node.下一结点 == null || node.下一结点.ExecutionStatus == ActivityExecutionStatus.Closed)

{

executionContext.CloseActivity();

}

else

{

 

node.下一结点.Closed += node_Closed;

executionContext.ExecuteActivity(node.下一结点);

}

 

}

    }

 

宿主

class Program

{

static void Main()

{

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);

workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));

instance.Start();

 

System.Console.Read();

 

}

 

static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)

{

System.Console.WriteLine("WorkflowIdled");

}

 

static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)

{

System.Console.WriteLine("Terminated" + e.Exception.Message);

}

 

static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)

{

System.Console.WriteLine("WorkflowCompleted");

}

 

}

 

使用

 

 

 

 

运行效果

 

 

 

本系列文章表

通过AEC解读WF的核心原理()Activity的副本

通过AEC解读WF的核心原理()通过AEC调用添加到引擎中的服务对像

通过AEC解读WF的核心原理()Execute方法Activity的入口

通过AEC解读WF的核心原理()AEC在内部执行childActivity

通过AEC解读WF的核心原理()实现一个从下向上执行的顺序容器

通过AEC解读WF的核心原理()创建复本ForEach循环

通过AEC解读WF的核心原理()一个实现Goto功能的Activity

通过AEC解读WF的核心原理()实现Switch功能的Activity

通过AEC解读WF的核心原理()一个实现Goto功能的Activity

通过AEC解读WF的核心原理()实现IEventActivity

通过AEC解读WF的核心原理()取消与取消处理器

通过AEC解读WF的核心原理(十一)(题外篇) WFWindows操作系统的对比

通过AEC解读WF的核心原理(十二)WorkflowQueue的提前提交

 

本系列完成

wxwinter 2008-6-18

posted @ 2008-06-18 11:56  WXWinter(冬)  阅读(5667)  评论(11编辑  收藏  举报