Site icon

Tasks in C#

Tasks in C# is known as 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#?

The .Net runtime executes a Task object asynchronously on a separate thread from the thread pool to perform the operation or work. The Task Scheduler is responsible to execute the Task , by default it requests a worker thread from the thread pool to execute it , 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.

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,

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.

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,

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.

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 .

Exit mobile version