简体   繁体   English

如何实现异步工作流程?

[英]How to implement a asynchronized workflow?

        WorkflowInvoker invoker = new WorkflowInvoker(new Workflow1());

        for (int i = 0; i < 10; i++)
        {
            //invoker.InvokeAsync(myOrders);
            IAsyncResult result = invoker.BeginInvoke(myOrders,new AsyncCallback(WorkflowCompletedCallback),order);
        }

I use the above code to implement asynchronized workflow. 我使用上面的代码来实现异步工作流。 I hope to run this workflow for 10 times and I have something similar with thread pool so the 10 workflow thread could run at the same time. 我希望将此工作流程运行10次,并且我与线程池有一些相似之处,因此10个工作流程线程可以同时运行。 The second doesn't need to wait for the first one finished its job. 第二个不需要等待第一个完成任务。 My workflow is very simple it will do some calculation and print several sentences on screen. 我的工作流程非常简单,它将进行一些计算并在屏幕上打印几个句子。 After I run the above code, I find it seems the 10 workflows are invoked one by one not as what I hoped to run at the same time. 运行上面的代码后,我发现似乎10个工作流程被一个一个地调用,而不是我希望同时运行。 What is the correct way to asynchronize workflow? 同步工作流的正确方法是什么? Thank you! 谢谢!

Update: After some feed back from others, I also try use workflowapplication to do this asynchronizely: 更新:在从其他人那里得到一些反馈之后,我也尝试使用工作流应用程序来异步执行此操作:

        WorkflowApplication wfApp = new WorkflowApplication(new Workflow1(), myOrders);
        for (int i = 0; i < 10; i++)
        {
            wfApp.Run();
        }

        /* Read the end time. */
        DateTime stopTime = DateTime.Now;
        Console.WriteLine(stopTime);
        // Duration
        TimeSpan duration = stopTime - startTime;
        Console.WriteLine("hours:" + duration.TotalHours);
        Console.WriteLine("minutes:" + duration.TotalMinutes);
        Console.WriteLine("seconds:" + duration.TotalSeconds);
        Console.WriteLine("milliseconds:" + duration.TotalMilliseconds);

Here is the running result: 4/8/2011 9:57:49 AM 4/8/2011 9:57:50 AM hours:6.27777777777778E-05 minutes:0.00376666666666667 seconds:0.226 milliseconds:226 Process Order Customer: 10 | 运行结果如下:4/8/2011 9:57:49 AM 4/8/2011 9:57:50 AM小时:6.27777777777778E-05分钟:0.00376666666666667秒:0.226毫秒:226流程订单客户:10 | Shipping:NextDay | 运送:次日| Total Price:250 | 总价:250 | Shipping Price:10 ProductID:1 | 运输价格:10产品ID:1 | Quantity:5 | 数量:5 | Price: 50 ProductID:2 | 价格:50产品编号:2 | Quantity:10 | 数量:10 | Price: 200 价格:200

It seems it is asynchronized but only one thread is actually running my workflow (not the main application thread). 看来它是异步的,但实际上只有一个线程在运行我的工作流(不是主应用程序线程)。 But from the output I only see one thread is running my workflow. 但是从输出中,我仅看到一个线程正在运行我的工作流。 How could I let 10 threads run the workflow at the same time? 如何让10个线程同时运行工作流程? Thank you! 谢谢!

If you're checking the IAsyncResult inside your loop, that will block until the async method has completed. 如果要在循环内检查IAsyncResult ,则它将阻塞直到async方法完成。 The IAsyncResult is provided so that you can use an asynchronous method synchronously, which is not what you're trying to do here. 提供了IAsyncResult ,以便您可以同步使用异步方法,这不是您要在此处执行的操作。

Your second example is wrong since WorkflowApplication.Run is blocking. 您的第二个示例是错误的,因为WorkflowApplication.Run被阻止。

As for your first example, see this article: http://www.codeproject.com/KB/WF/OperationWorkflowInvoker4.aspx 对于第一个示例,请参见本文: http : //www.codeproject.com/KB/WF/OperationWorkflowInvoker4.aspx

especially: http://www.codeproject.com/KB/WF/OperationWorkflowInvoker4/Operat1.jpg 特别是: http : //www.codeproject.com/KB/WF/OperationWorkflowInvoker4/Operat1.jpg

It appears that WorkflowInvoker uses a dispatcher strategy which very well might be single threaded. 看来WorkflowInvoker使用的调度程序策略很可能是单线程的。 Investigate in this or do your own thread scheduling 对此进行调查或自己进行线程调度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM