如何实现异步工作流程?

发布于 2024-10-31 11:35:21 字数 1483 浏览 0 评论 0原文

        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);
        }

我使用上面的代码来实现异步工作流程。我希望运行此工作流程 10 次,并且我有类似的线程池,因此 10 个工作流程线程可以同时运行。第二个不需要等待第一个完成其工作。我的工作流程非常简单,它将进行一些计算并在屏幕上打印几个句子。运行上面的代码后,我发现似乎10个工作流程被一一调用,而不是我希望同时运行的。异步工作流程的正确方法是什么?谢谢你!

更新:在其他人的一些反馈之后,我还尝试使用工作流应用程序异步执行此操作:

        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);

这是运行结果: 2011 年 4 月 8 日上午 9:57:49 2011 年 4 月 8 日上午 9:57:50 小时:6.27777777777778E-05 分钟:0.00376666666666667 秒:0.226 毫秒:226 处理订单 客户:10 |运送:次日 |总价:250 |运费:10 产品编号:1 |数量:5 |价格:50 产品编号:2 |数量:10 |价格:200

看起来它是异步的,但实际上只有一个线程正在运行我的工作流程(不是主应用程序线程)。但从输出中我只看到一个线程正在运行我的工作流程。如何让 10 个线程同时运行工作流程?谢谢你!

        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. 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. 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 | Shipping:NextDay | Total Price:250 | Shipping Price:10
ProductID:1 | Quantity:5 | Price: 50
ProductID:2 | Quantity:10 | Price: 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? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-11-07 11:35:21

如果您在循环内检查 IAsyncResult,则该操作将会阻塞,直到异步方法完成为止。提供 IAsyncResult 是为了让您可以同步使用异步方法,但这不是您在此处尝试执行的操作。

If you're checking the IAsyncResult inside your loop, that will block until the async method has completed. The IAsyncResult is provided so that you can use an asynchronous method synchronously, which is not what you're trying to do here.

横笛休吹塞上声 2024-11-07 11:35:21

您的第二个示例是错误的,因为 WorkflowApplication.Run 正在阻塞。

至于您的第一个示例,请参阅这篇文章: http://www.codeproject.com/KB /WF/OperationWorkflowInvoker4.aspx

尤其是: http://www.codeproject .com/KB/WF/OperationWorkflowInvoker4/Operat1.jpg

看起来,WorkflowInvoker 使用的调度程序策略很可能是单线程的。研究这个或者做你自己的线程调度

Your second example is wrong since WorkflowApplication.Run is blocking.

As for your first example, see this article: http://www.codeproject.com/KB/WF/OperationWorkflowInvoker4.aspx

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

It appears that WorkflowInvoker uses a dispatcher strategy which very well might be single threaded. Investigate in this or do your own thread scheduling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文