开发者

How to implement a asynchronized workflow?

        WorkflowInvoker invoker = new WorkflowInvoker(开发者_JAVA技巧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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜