上一节我们做了个简单的测试证明了Parallel并不是真正意义上的并行工作流,并且分析了它的缺陷。

在.net framework 4里面有很多关于并行计算的组件,现在我们就要利用微软TPL(The Task Parallel Library)中的System.Threading.Tasks.Task类型来实现并行的分支。要定制异步工作流活动,我们的新类必须继承于.net WF中的基类AsyncCodeActivity

我们重写一下PartDBReplicationActivity,

public class PartDBReplicationAsyncActivity : AsyncCodeActivity
{
        public InOutArgument<Order> OrderInActivity { get; set; }

        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            Order order = context.GetValue(OrderInActivity);
            Task worker = Task.Factory.StartNew(
                (o) =>
                {
                    order.Status = OrderStatus.InProcess;
                    //Call Part service
                    Thread.Sleep(5000);
                }, state);
            worker.ContinueWith(task => callback(task));
            return worker;
        }

        protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {
           
        }
}

再看新的VehicleReplicationAsyncActivity 

public class VehicleReplicationAsyncActivity : AsyncCodeActivity
 {
        public InOutArgument<Order> OrderInActivity { get; set; }

        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            //Console.WriteLine("VehicleDBReplication AsyncActivity starts at:{0} \r\n", DateTime.Now.ToString());
            Order order = context.GetValue(OrderInActivity);
            Task worker = Task.Factory.StartNew(
                (o) =>
                {                  
                    order.Status = OrderStatus.InProcess;
                    //Call Part service
                    Thread.Sleep(5000);
                    //Console.WriteLine("VehicleDBReplication AsyncActivity ends at {0}.\r\n", DateTime.Now.ToString());
                }, state);
            worker.ContinueWith(task => callback(task));
            return worker;
        }

        protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {

        }
 }

我们修改main里面的代码让它执行新的Close Order流程

class Program
{
static void Main(string[] args)
{
DateTime dtStart
= DateTime.Now;
Console.WriteLine(
"CloseOrder workflow starts at:{0}", dtStart);
Console.WriteLine(
"--------------------------------------------------------------");
//WorkflowInvoker.Invoke(new CloseOrderWorkflow());
WorkflowInvoker.Invoke(new CloseOrderParallelWorkflow());
Console.WriteLine(
"--------------------------------------------------------------");
DateTime dtEnd
= DateTime.Now;
Console.WriteLine(
"CloseOrder workflow ends at:{0}", dtEnd.ToString());
Console.WriteLine(
"Total time elapsed in seconds :{0}",( dtEnd - dtStart).Seconds.ToString());
Console.Read();
}
}

看结果

现在整个流程只花了5秒钟,我们初步完成基于微软并行解决方案的并行工作流。

至此我们实现的并发工作流要求分支之间线程安全,业务上没有耦合和次序关系。

我们实现的基于并行计算并发工作流活动也存在一些缺陷:可扩展性不高,开发人员过于底层细节的关注,封装性不好等等..

后面的任务是要完成一个真正意义上的可扩展的并发Parallel Activity,用户只要拖拽这个工作流活动到Visual Studio里面,设置一些参数,比如绑定处理的真正任务,那么就无需和Parallel.Task底层对象打交道,只需要对业务逻辑的实现。 

示例代码:

https://files.cnblogs.com/huyq2002/ParallelTask.zip


posted on 2011-05-25 14:38  胡以谦  阅读(1475)  评论(11编辑  收藏  举报