Task in C# Asynchronous Programming

Welcome to the Asynchronous Programming in C# 5.0 article series. The previous two articles erxplained what asynchronous programming is and how to implement it in C# 5.0. We also have seen various return types of asynchronous functions. To read all those, please visit the following links. 
This article explains Tasks in asynchronous programming. If we follow the previous two articles (or if you are already familiar with Tasks) then you will find that we are returning a Task from an asynchronous function. But the question is, what is a Task? Let me give a single-line answer: "A Task is a basic unit of the Task Parallel Library (TPL)". I know that that single-line answer is not enough for understanding what a Task is. Don't worry; we will dig into it more since this article is dedicated to Tasks. (Not office tasks, Tasks of asynchronous programming!!) Ok, let's be serious and proceed to the topic.
 
So, a task is nothing but a unit of work. Try to map them with real life task to understand better. 
  • Task can run/start: Real-life tasks can run/start (read proceed).
  • Task can wait: Real-life tasks wait too (my friend waited for seven days to get feedback of his first proposal, though it was negative.)
  • Task can cancel: No need to provide an example, you often cancel your task.
  • Task can have a child Task: Yes, there are subtasks in people's lives.
  • So, those are the features (better to say properties) of Tasks in asynchronous programming. A Task can only run from its start to its finish, you cannot run the same task object two times. Now , the question is , what is the solution for running the same task more than once? The answer is you will need to create another Task object to run the same task.
Ok, let's try one small example to understand Tasks.
 
Have a look at the following code. Here we will create an object of the Task class.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;     
  6.   
  7. namespace Asynchronious    {    
  8.     class Program    
  9.     {    
  10.         public static void Main(String [] args)    
  11.         {    
  12.             Task t = new Task(    
  13.                 () => {    
  14.                        System.Threading.Thread.Sleep(5000);    
  15.                        Console.WriteLine("Huge Task Finish");     
  16.                      }    
  17.                 );     
  18.   
  19.             //Start the Task    
  20.             t.Start();    
  21.             //Wait for finish the Task    
  22.             t.Wait();    
  23.             Console.ReadLine();    
  24.         }    
  25.     }    
  26. }  

We are calling the Start() method to start the Task. After that we are calling the Wait() method that implies that we are waiting for the task to finish. Here is sample output.


Asynchronous1.jpg

 
How to Wait for a Task?
 
Let's try to understand how to delay (or sleep) a Task for a while. Have a look at the following example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Asynchronious  
  8. {  
  9.     class Program  
  10.     {  
  11.         public static void Main(String [] args)  
  12.         {  
  13.             Task t = new Task(  
  14.                 () => {  
  15.                        System.Threading.Thread.Sleep(5000);  
  16.                        Console.WriteLine("Huge Task Finish");   
  17.                      }  
  18.                 );   
  19.   
  20.             //Start the Task  
  21.             t.Start();  
  22.   
  23.             //Wait for 1 second  
  24.             bool rValue = t.Wait(1000);  
  25.             Console.WriteLine("Main Process Finished");  
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29. }  
In this example we are waiting to finish a huge task for one second even after it has actually finished. So, we learn how to continue to wait for a Task. Here is the output screen.
 
Asynchronous2.jpg
 
Implement Child Task
 
Here we will implement a child Task of another Task. Have a look at the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Asynchronious  
  7. {  
  8. class Program  
  9. {  
  10. public static void Main(String [] args)  
  11. {  
  12. Task Parent = new Task(  
  13. () => {  
  14. Task Child = new Task(  
  15. ()=> {  
  16. System.Threading.Thread.Sleep(2000);  
  17. Console.WriteLine("Inner Task Finish");  
  18. },  
  19. TaskCreationOptions.AttachedToParent  
  20. );  
  21.   
  22. //Start Child Task  
  23. Child.Start();  
  24. System.Threading.Thread.Sleep(2000);  
  25. Console.WriteLine("Outer Task Finish");  
  26. }  
  27. );  
  28.   
  29. //Start the Task  
  30. Parent.Start();  
  31. Parent.Wait();  
  32. Console.ReadLine();  
  33. }  
  34. }  
  35. }  
At first we created a Parent Task that contains another child Task within it. The innermost and outermost Tasks will sleep for two seconds. We are running a child Task within the object of a parent Task.
 

Asynchronous3.jpg

 
Now, let's analyze the output. We see that at first the outer Task is finishing and then the innermost Task finishes. Why is that? It is happening due to an asynchronous call. The outer Task is running the innermost Task but not waiting for it and this is the beauty of an asynchronous call.
 
Get Status of Task
 
We can detect the status of any Task. Let's see in the following example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading;  
  6. using System.Threading.Tasks;  
  7. namespace Asynchronious  
  8. {  
  9. class Program  
  10. {  
  11. public static void Main(String [] args)  
  12. {  
  13. Task t = new Task(  
  14. () => {  
  15. System.Threading.Thread.Sleep(5000);  
  16. });  
  17.   
  18. t.Start();  
  19. t.Wait();  
  20. Console.WriteLine(t.Status);  
  21. Console.WriteLine("End of Main");  
  22. Console.ReadLine();  
  23. }  
  24. }  
  25. }  
Here we are starting the Task and then we are waiting for the Task to be complete. In the next line we are checking the Status of the Task using the Status property of the t (Task) object. Now a question may arise. What is the purpose of checking the status? There are many things to do, so we can run another Task depending on the status of another Task. Let's see the output.
 

Asynchronous4.jpg

 
 
The status is showing RunToCompletion. It means that the Task is running currently and it will complete.
 
Few more properties of Task class
 
Let's check a few more properties of the Task class. Have a look at the following example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading;  
  6. using System.Threading.Tasks;  
  7. namespace Asynchronious  
  8. {  
  9. class Program  
  10. {  
  11. public static void Main(String [] args){  
  12.   
  13. Task t = new Task(  
  14. () => {  
  15. System.Threading.Thread.Sleep(5000);  
  16.   
  17. });  
  18.   
  19. t.Start();  
  20. Console.WriteLine("Cancelled:- " + t.IsCanceled);  
  21. Console.WriteLine("Completed:- " + t.IsCompleted);  
  22. Console.WriteLine("Folted:- " + t.IsFaulted);  
  23. Console.WriteLine("End of Main");  
  24. Console.ReadLine();  
  25. }  
  26. }  
  27. }  
Here we will check a few statuses of the Task object. For example we are interested in checking for the Cancel, Completed and Failed statuses of the Task. In the example all are False. That means
  • Cancelled: The Task is not Cancelled
  • Completed: It is not completed (still running)
  • Faulted: There is no error or exception to run this Task.
Asynchronous5.jpg 
 
Conclusion
 
In this article we have learned what a Task is and various properties of Task. In future articles we will concentrate on exception handling in asynchronous programming. Keep on reading this series. Hey..!! Are you still reading? Then that means both of us love asynchronous programming. Have a nice day.
 

Author
Sourav Kayal
0 27.7k 24m
Next » Exception Handling in C# Asynchronous Programming