Shadman Kudchikar

Getting Started With C# Task


C# Task

To get started with Tasks in .NET C# you have to create an object of Task Class in C#, which is available in namespace System.Threading.Tasks and provide the code, to be executed within the task, as the task action parameter.

C# Task Example

Let’s see an example,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _01_Getting_Started_With_Task
    class Program
        static void Main(string[] args)
            //create task object and pass anonymous method 
            //to task constructor parameter as work to do 
            //within the task
            Task t = new Task(() =>
                  for (int i = 0; i < 100; i++)
                      //print task t thread id
                      var threadId = Thread.CurrentThread.ManagedThreadId;
                      Console.WriteLine("Task Loop Current Thread Id:" + threadId);

            //start task t execution

            for (int i = 0; i < 100; i++)
                //print main thread id
                var threadId = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine("Main Loop Current Thread Id " + threadId);

            //wait for task t to complete its execution

            Console.WriteLine("Press enter terminate the process!");

Let’s see whats happening in the above program line by line.

On line 17 we are creating the Task t and provide the code to execute. But, Task t start its execution after calling t.Start() method at the line 27.


From this point, our program is split into two programs that are executing in the parallel codestream also known as a fork or split in program.

So basically code in task and code below the task’s start method, both are executing together. You can’t see this but this is definitely happening and you have to imagine this, that now your program is executing two parallel codestreams.

Also, you must have noticed that we passed an anonymous method to Task constructor parameter, as a code to be executed within the task. This we can do because Task Class constructor take Action as a parameter which is nothing but a delegate with void return type.

Execution Model Of A Task

Let’s discuss basic execution model of a task,

In the example above, we created a task and provide basic operation to be performed by the task.

So this task operation is executed in a separate thread. Basically, each task we create are executed within their own thread.

In a single core machine, this won’t be efficient as there will be lot of context switching between this threads and they will endup using the same single-core processor.


In a multi-core processor, which is the case nowadays, this will be very efficient as this threads will get distributed within the multi-core machine (each thread executing in separate processor) and thus will perform better than a sequential program.

Let’s dive deeper into the execution model of task in the next chapter.

comments powered by Disqus