Tasks in C# is nothing but an operation or a work that executes in asynchronous manner. It was introduced in .Net framework 4.0 to support asynchronous functionality, in-fact the Task Parallel Library which is known as TPL is based on the concept of Tasks.

What is a Task in C# ?

In C# Task is basically used to implement asynchronous programming model, the .Net runtime executes a Task object asynchronously on a separate thread from the thread pool.

The Task Scheduler in .Net framework is responsible to execute the Task , by default it requests a worker thread from the Thread Pool to execute the Task . Here, the thread pool determines, whether to create a new thread or reuse an existing thread from the thread pool to execute the operation.

Creating Tasks in C# ?

To create a Task in C#, first you need to import System.Threading.Tasks namespace in to your program, then you can use the Task class to create object and access its properties.

Example – 1 : Creating Tasks in C# using Task class and Start method

In below example, we have created a Task object as Task t , passing the method PrintEvenNumbers; in the constructor, then we invoke the Task t by t.Start(); method.

When we run above example, it generates below output, here the task executes in a separate thread and prints the even numbers on the console window.

Tasks Out Put 1.0

Different ways of creating Tasks in C#:

There are various ways available in C#.Net 4.0 to create a Task object. Please find some of the different ways as follows.

Task Creation using Factory method :

We can also use Task.Factory() method to creates a task instance and invoke it in a single line of code, as follows.

Task creation using Action Generic Delegate :

You can use below syntax to create a task using Action type (Generic Delegate) , In below code example, we have created a task object and then invoking it.

Task creation using a Delegate :

In below code sample, we are using delegate keyword to create a task instance and then invoking it.

Similarly, we can also use Task.Run() method to create a task object and invoke it in single line, as follows.

Task creation using Lambda and named method :

In below code sample, we are using lambda and named method expression to create a task instance and then invoking it.

Similarly we can also use Task.Run() method to create a task object and invoke it in single line, as follows.

Example – 2 : Example of a Task creation and execution flow

Lets go through an example and see the Task creation and execution in detail.

In below example,

  • We have created a method as PrintOddEvenNumbers that prints odd and even numbers.
  • In the Main method, we have created a task instance Task t = new Task(PrintOddEvenNumbers); , assigned it to the method and then invoking it by calling t.start() .

When we run above example, then below out put will be generated. If you look at the console output, after the task executes successfully, it prints odd and even numbers less than 10 on the console window.

Tasks in C# Out Put 1.1

In the above output window, if you look at the messages, then its clear that the Task executed on a separated Child thread (Thread  Number: 3) , where as Main method executed on main thread (Thread ID : 1).

Also If you observe above result,

  • Both threads (Main and Child) started its execution simultaneously (asynchronously), however the Main thread didn’t wait for the Child thread to complete.
  • The Child thread continued its execution until it finishes its task even after completion of Main thread execution.

If you want to make the Main thread execution to wait until the other tasks complete its execution, you can do that by using Task.Wait method. To know more about this, please check my next article Tasks in C# Extended .

Key Points of Tasks in C#:

Following are some of the key points that you need to remember about tasks.

  • Task is a high-level of abstraction over thread, internally it uses thread from a thread pool to perform operation asynchronously.
  • We need to import System.Threading.Tasks in our program to work with Task objects.
  • If you want to make the Main thread execution to wait until child task finishes its execution, you can do that by using Task.Wait method.

Thanks for your your time 🙂 , please provide your feedback and suggestions. please check out our next article Tasks in C# Extended  , here we will be discussing on .

  • How to wait for Task to complete ?
  • Get return value from Tasks ?