Share via


Exercise 3: Use the Generic Task Class to Create and Run Tasks that Return a Value

As you can see, Tasks are useful for launching a unit of functionality in a parallel environment; they also provide a mechanism for returning values as a result of executing the unit.

To demonstrate this, you are going to create a new instance of a Task<decimal> and then use the static Task.Factory.StartNew() method to execute the GetPayrollDeduction() method in a manner that allows you to capture its return value.

Task 1 – Capturing a Task’s Return Value

  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\Ex03-UseTaskResult\begin.(choosing the folder that matches the language of your preference) Optionally, you can continue working with the solution you created in the previous exercise.
  3. Replace the current method calls from Main(), with a call to Ex3Task1_TaskReturnValue().

    C#

    static void Main(string[] args)
    FakePre-72837c70cfdf4d99959176280be6ab95-2e5abadefbdb4650a07d701323495610FakePre-da5c3813378f43ec9cb31d8da6271ed4-600a2af277264c5cafe546d503859d42FakePre-9f7eb4ce6e2540a8bd15c6a13cee4569-56a834b5d1884eb089f0117744e7e1ca Ex3Task1_TaskReturnValue();FakePre-ee3303c2a2964eaf8dbe94fe46deea15-2f8630d6083b418ea16c965e56911633FakePre-0c60f94a53ff428f99b2426f8753274d-41906fb9cc784249871a400184be255e

    Visual Basic

    Sub Main(ByVal args() As String)
    FakePre-b4b053887e514491b0e3d32e8cd54001-252723e9c3de405594840669a9ad48bcFakePre-6507c08792174beb9a4376498fe18a34-c539c89275834ae9a7db1db6eca8c0f1 Ex3Task1_TaskReturnValue()FakePre-116ed7a6d91449d2a51668c4a0d7dcc6-500a442178354521a581f5d4a625140bFakePre-b313f1226298474799e3c75551d9a1b6-fb996f956d6f43f4880aff40bc62bacc

  4. Add the Ex3Task1_TaskReturnValue method to Program.cs (C#) or Module1.vb (Visual Basic):

    (Code Snippet – Intro to Parallel Extensions Lab - Ex3 Ex3Task1_TaskReturnValue CSharp)

    C#

    private static void Ex3Task1_TaskReturnValue() { Console.WriteLine("Calling parallel task with return value"); var data = Task.Factory.StartNew(() => PayrollServices.GetPayrollDeduction(employeeData[0])); Console.WriteLine("Parallel task returned with value of {0}", data.Result); }

    (Code Snippet – Intro to Parallel Extensions Lab - Ex3 Ex3Task1_TaskReturnValue VB)

    Visual Basic

    Private Sub Ex3Task1_TaskReturnValue() Console.WriteLine("Calling parallel task with return value") Dim data As Task(Of Decimal) = Task.Factory.StartNew(Function() PayrollServices.GetPayrollDeduction(employeeData(0))) Console.WriteLine("Parallel task returned with value of {0}", data.Result) End Sub

    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