In this post I am going to talk about creating a task and various method of task library which interacts with task execution in C#

Create a task

Creating a task is done by providing the method of work to the lambda expression. Start method starts the task and wait method waits for the task to complete.

Creating and running a task can be achieved using Run method of Task alone. Below code shows usage of it.

Return a value from a task

A task can be created that will return a value. Result property on the task makes the program to wait for completion to deliver the result. Below program shows a task returning integer. Task.Run method uses the TaskFactory.StartNew method to create and start the task, using the default task scheduler that uses the .NET framework thread pool.

WaitAll

The Task.WaitAll method can be used to pause the program to wait for all the tasks to complete. Below program also demonstrates the issue with passing the loop control variable to lambda expression. Because of closures if we use the loop control variable to pass to the lambda method every taskNum will have the end value of the loop. This is the reason we store it in local variable. The WaitAll method can also be used to catch exceptions thrown by any task. The exception thrown would also be an aggregate exception. Below program shows the usage of WaitAll. WaitAny method can also be used if you want the program to resume after any number of concurrent tasks completes.

Continuation Tasks

A continuation task can be nominated to start when an existing task finishes. If the previous task produced a result it can be supplied as an input to the continuation task. Continuation tasks create a pipeline of operations with each successive stage starting when preceding ends. The lambda expression executing continuation task is provided with a reference to previous task which it can use to determine if the previous task has completed successfully. There are also two overloads of where we can specify the continuation task to execute only when the previous task has RanToCompletion or Failed. Below code makes use of these overloads.

Child tasks

Code running inside a parent task can create other tasks. These child tasks can execute independently of the parent in which they are created which are called detached child tasks. A parent task can specify if child tasks need to be attached, in that case parent task will not complete till all attached child tasks are run. Attached Child tasks can be created by making use of StartNew method of TaskFactory which takes in three arguments which are lambda expression for the task, state object and TaskCreationOptions to specify if a task has to be created as attached or detached task. Task.Run method has the option DenyChildAttach option set, and therefore can’t have attached tasks. Below is a code making use of child tasks.

GitHub Link: Tasks
I couldn’t go to gym today so my 100DaysOfFitness is slightly disrupted, but nevertheless i am still going to continue that from tomorrow. #4DaysOf100DaysOfCode
Last modified: March 17, 2019

Author

Comments

Write a Reply or Comment

Your email address will not be published.