Task Factory and,Task Parallelism (Task Parallel Library) in C#

Represented by:

Important Note:

  • Runs the task immediately after creating the task .This type of execution is known as Task Parallelism.

  • Runs the task in a pre-defined order. This type of execution is known as Continuation Tasks.

  • Running the task by pair. That is in Asynchronous programming model.

Now let’s look deep into Task Parallelism.

  • Task parallel library is used to perform asynchronous operation.

  • The task that are invoked by Task Parallelism is invoked by are independent of one another.
    1. Parallel.Invoke(() => Console.WriteLine("Perform Operatrion 1"), () =>Console.WriteLine("Perform Operatrion 2"));  
  • The Task are separated by “,”. They are independent of one another.

  • These task are actually queued in ThreadPool.

  • It provides many enhancements such as waiting, cancellation, continuations, exception handling, detailed status, custom scheduling, and more.

  • It takes Action delegate as input, that is the return type is always void.

  • Task.Run method create and start a task in one operation, that is in one step.

  • TaskFactory.StartNew is also used to create and start a task in one operation.

Code sample

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Task_Parallel_Library {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             runoperation obj = new runoperation();  
  10.             Console.WriteLine("Creating and running tasks implicitly");  
  11.             obj.parallelinvoke_implicitly();  
  12.             Console.WriteLine();  
  13.             Console.WriteLine("Creating and running tasks explicity");  
  14.             obj.parallelinvoke_explicity_01();  
  15.             Console.WriteLine();  
  16.             Console.WriteLine("Creating and running tasks explicity by referencing action delegate");  
  17.             obj.parallelinvoke_explicity_03();  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21.     class runoperation {  
  22.         static runoperation() {}  
  23.         public void parallelinvoke_implicitly() {  
  24.             //Creating and running tasks implicitly  
  25.             // This way of running mutiple task at same time is known as implicit task   
  26.             // both operation are exxecuted parallelly  
  27.             //Retun type is always void for parallel invoke. In technical term the input of parallel invoke is always action delegate  
  28.             Parallel.Invoke(() = > Console.WriteLine("Perform Operatrion 1"), () = > Console.WriteLine("Perform Operatrion 2"));  
  29.         }  
  30.         public void parallelinvoke_explicity_01() {  
  31.             Parallel.Invoke(() = > dowork("Task- One"), () = > dowork("Task- Two"), () = > dowork("Task- Three"));  
  32.         }  
  33.         private void dowork(string input) {  
  34.             Console.WriteLine("I have been called by '{0}'", input);  
  35.         }  
  36.         public void parallelinvoke_explicity_03() {  
  37.             // parallel invoke with action delegate as input  
  38.             Parallel.Invoke(action1);  
  39.         }  
  40.         /// <summary>  
  41.         /// Following is an action delegate with one input of type string  
  42.         /// </summary>  
  43.         Action action1 = new Action(delegate {  
  44.             Console.WriteLine("Hi.... I am action Delegate.... I am inline coded with anonymous type");  
  45.         });  
  46.     }  
  47. }  

 

Next Recommended Reading Task vs Thread differences in C#