Ex1Task1_UseParallelForMethod()
... End Sub
10. Build and run the application. 11. You should observe that the employees are not necessarily processed in the order of their IDs. You’ll also notice that multiple calls to the GetPayrollDeduction() method are made before the first call returns. And finally, you should observe that by running the calls in parallel, the entire job completed much faster than when run in serial. Figure 2 Output from parallel calls to a long running service
Note: Because the loop is run in parallel, each iteration is scheduled and run individually on whatever core is available. This means that the list is not necessarily processed in order, which can drastically affect your code. You should design your code in a way that each iteration of the loop is completely independent from the others. Any single iteration should not rely on another in order to complete correctly.
12. The Parallel Extensions library also provides a parallel version of the foreach structure. The following code demonstrates the non-parallel way to implement this structure. Add the following method to Program.cs (C#) or Module1.vb (Visual Basic). (Code Snippet – Intro to Parallel Extensions Lab - Ex1 Ex1Task1_StandardForEach CSharp) C#
|