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!
If you’re checking the
IAsyncResultinside your loop, that will block until the async method has completed. TheIAsyncResultis provided so that you can use an asynchronous method synchronously, which is not what you’re trying to do here.