Parallelism in .NET – Part 14, The Different Forms of Task
Posted
by Reed
on Reed Copsey
See other posts from Reed Copsey
or by Reed
Published on Thu, 18 Mar 2010 00:56:57 +0000
Indexed on
2010/12/06
17:00 UTC
Read the original article
Hit count: 1209
Before discussing Task creation and actual usage in concurrent environments, I will briefly expand upon my introduction of the Task class and provide a short explanation of the distinct forms of Task. The Task Parallel Library includes four distinct, though related, variations on the Task class.
In my introduction to the Task class, I focused on the most basic version of Task. This version of Task, the standard Task class, is most often used with an Action delegate. This allows you to implement for each task within the task decomposition as a single delegate.
Typically, when using the new threading constructs in .NET 4 and the Task Parallel Library, we use lambda expressions to define anonymous methods. The advantage of using a lambda expression is that it allows the Action delegate to directly use variables in the calling scope. This eliminates the need to make separate Task classes for Action<T>, Action<T1,T2>, and all of the other Action<…> delegate types. As an example, suppose we wanted to make a Task to handle the ”Show Splash” task from our earlier decomposition. Even if this task required parameters, such as a message to display, we could still use an Action delegate specified via a lambda:
// Store this as a local variable string messageForSplashScreen = GetSplashScreenMessage(); // Create our task Task showSplashTask = new Task( () => { // We can use variables in our outer scope, // as well as methods scoped to our class! this.DisplaySplashScreen(messageForSplashScreen); });
This provides a huge amount of flexibility. We can use this single form of task for any task which performs an operation, provided the only information we need to track is whether the task has completed successfully or not. This leads to my first observation:
Use a Task with a System.Action delegate for any task for which no result is generated.
This observation leads to an obvious corollary: we also need a way to define a task which generates a result. The Task Parallel Library provides this via the Task<TResult> class.
Task<TResult> subclasses the standard Task class, providing one additional feature – the ability to return a value back to the user of the task. This is done by switching from providing an Action delegate to providing a Func<TResult> delegate. If we decompose our problem, and we realize we have one task where its result is required by a future operation, this can be handled via Task<TResult>. For example, suppose we want to make a task for our “Check for Update” task, we could do:
Task<bool> checkForUpdateTask = new Task<bool>( () => { return this.CheckWebsiteForUpdate(); });
Later, we would start this task, and perform some other work. At any point in the future, we could get the value from the Task<TResult>.Result property, which will cause our thread to block until the task has finished processing:
// This uses Task<bool> checkForUpdateTask generated above... // Start the task, typically on a background thread checkForUpdateTask.Start(); // Do some other work on our current thread this.DoSomeWork(); // Discover, from our background task, whether an update is available // This will block until our task completes bool updateAvailable = checkForUpdateTask.Result;
This leads me to my second observation:
Use a Task<TResult> with a System.Func<TResult> delegate for any task which generates a result.
Task and Task<TResult> provide a much cleaner alternative to the previous Asynchronous Programming design patterns in the .NET framework. Instead of trying to implement IAsyncResult, and providing BeginXXX() and EndXXX() methods, implementing an asynchronous programming API can be as simple as creating a method that returns a Task or Task<TResult>. The client side of the pattern also is dramatically simplified – the client can call a method, then either choose to call task.Wait() or use task.Result when it needs to wait for the operation’s completion.
While this provides a much cleaner model for future APIs, there is quite a bit of infrastructure built around the current Asynchronous Programming design patterns. In order to provide a model to work with existing APIs, two other forms of Task exist. There is a constructor for Task which takes an Action<Object> and a state parameter. In addition, there is a constructor for creating a Task<TResult> which takes a Func<Object, TResult> as well as a state parameter. When using these constructors, the state parameter is stored in the Task.AsyncState property.
While these two overloads exist, and are usable directly, I strongly recommend avoiding this for new development. The two forms of Task which take an object state parameter exist primarily for interoperability with traditional .NET Asynchronous Programming methodologies. Using lambda expressions to capture variables from the scope of the creator is a much cleaner approach than using the untyped state parameters, since lambda expressions provide full type safety without introducing new variables.
© Reed Copsey or respective owner