End Sub. 5. Build and run the application.
5. Build and run the application. 6. You should observe that when run in parallel, some of the tasks might not complete execution until after the Ex2Task1_NativeParallelTasks method has exited and control has returned to Main. Because of this, the output time also does not reflect the total processing time as it is likely the tasks have not completed before returning to Main. Figure 7 Output from running several Tasks in parallel
Task 2 – Using the Wait() and WaitAll() Methods The benefit of executing tasks in parallel is faster execution and the ability to leverage multi-core processors. However, you should also notice that the current implementation introduces the possibility the main application could exit before the thread processing the task finishes. You can handle this possible situation by invoking the Wait() method on the individual Task objects. This causes the main thread to wait until the indicated tasks are complete before continuing on to the next instruction. 1. Replace the current method calls from Main(), with a call to Ex2Task2_WaitHandling(). This code will add wait handling to your example. C# static void Main(string[] args) { ... // Methods to call
|