End Sub. Note: The value is captured by inspecting the data.Result property
Note: The value is captured by inspecting the data.Result property. If the task has completed when the Result property is invoked then it will return the captured value immediately, otherwise it will block the executing code until the task has completed and the value can be retrieved. In the above example, you are accessing the Result property right away, which is not the ideal situation. Where Task<T> becomes very useful is when you are firing off units of works where you will not be retrieving the returned values until a later time.
5. Build and run the application. 6. You should observe that the task completes and a return value is provided. Figure 12 Output from running a Task to capture a return value
Next Step: Exercise 4: Parallelize LINQ Queries using PLINQ
Exercise 4: Parallelize LINQ Queries using PLINQ Developers can optimize their LINQ queries to execute in a parallel environment by utilizing Parallelized LINQ (PLINQ). The Parallel Extensions library offers many different ways to implement parallelism in LINQ queries. PLINQ provides you with the System.Linq.ParallelEnumerable class which offers functionality similar to the System.Linq.Enumerable class.
Task 1 – Using the ParallelEnumerable Class’ Static Methods to Parallelize LINQ In this task, you will continue to use the same solution as the previous exercises. 1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010. 2. Open the solution file ParallelExtLab.sln located under Source\Ex04-PLINQ\begin(Choosing the folder that matches the language of your preference.) Optionally, you can continue working the solution you created in the previous exercise. 3. Replace the current method calls from Main(), with a call to Ex4Task1_PLINQ(). C# static void Main(string[] args) { ... // Methods to call
|