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 an operation.

The Task Scheduler requests a worker thread from the thread pool , the thread pool manager checks if there is any idle thread available or need to create a new thread from the thread pool to perform 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.
1 2 3 4 |
//Create a task instance Task t = new Task(PrintEvenNumbers); //Start the task t.Start(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Threading.Tasks; namespace TaskInCshap { class Program { static void Main(string[] args) { //Create a task instance Task t = new Task(PrintEvenNumbers); //Start the task t.Start(); Console.Read(); } public static void PrintEvenNumbers() { Console.WriteLine("Even numbers less than 10"); for (int i = 0; i <= 10; i++) if(i%2 == 0) Console.WriteLine("Number : {0}", i); } } } |
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 :
You can use Task.Factory() method to creates a task instance and invoke it in a single line of code, as follows.
1 2 |
//Create a task and invoke it Task.Factory.StartNew(PrintEvenNumbers); |
Task creation using Action Generic Delegate :
You can use below syntax to create a task using Action type (Generic Delegate).
1 2 3 4 |
//Create a Task using Action delegate Task t = new Task(new Action(PrintEvenNumbers)); //Invoke Task t.Start(); |
Task creation using a Delegate :
In below code sample, we are using delegate keyword to create a task instance and then invoking it.
1 2 3 4 |
//Create a Task using delegate Task t = new Task(delegate { PrintEvenNumbers(); }); //Invoke Task t.Start(); |
Similarly, we can also use Task.Run() method to create a task object and invoke it in single line, as follows.
1 2 |
//Invoke it directly using Run method Task t = Task.Run(delegate { PrintEvenNumbers(); }); |
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.
1 2 3 4 |
//Creating a Task instance using lambda and named method Task t = new Task(() => PrintEvenNumbers()); //Invoke Task t.Start(); |
Similarly we can also use Task.Run() method to create a task object and invoke it in single line, as follows.
1 2 |
//Invoke it directly using Run method Task t = Task.Run(() => PrintEvenNumbers()); |
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() .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Threading.Tasks; namespace TaskInCshap { class Program { static void Main(string[] args) { //Create a task instance Task t = new Task(PrintOddEvenNumbers); t.Start(); Console.WriteLine("In Main Method, Finished work on thread : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId); Console.Read(); } public static void PrintOddEvenNumbers() { Console.WriteLine("Even and Odd numbers less than 10"); //Print even numbers for (int i = 0; i <= 10; i++) if(i%2 == 0) Console.WriteLine("Even Number : {0}", i); //Print odd numbers for (int i = 0; i <= 10; i++) if (i % 2 != 0) Console.WriteLine("Odd Number : {0}", i); Console.WriteLine("In Child Method, Finished work on thread : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId); } } } |
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,
- 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 ?
Very helpful blog 👍
Amazing explanation Task article.