Different Ways To Create Task Parallel Library (TPL Threads)

The Task Parallel Library explains the concept of "Task". It executes Tasks in parallel way. It has great advantages over thread and thread pool. This library has been released in version 4.0 of the .NET framework.
 
The following are some benefits of using Tasks:
  1. It uses system resources more efficiently.
  2. It provides more programming control than threads.
  3. It also includes other concept like Future. Future returns result.

Namespace

  1. using System.Threading.Tasks  
Creating Tasks with different ways 

1. Direct  and common way
  1. #region Most Direct Way  
  2.   
  3.      /// <summary>  
  4.      /// Most direct way to create thread.  
  5.      /// </summary>  
  6.      public void MostDirectWay()  
  7.      {  
  8.          Task.Factory.StartNew(() => { Console.WriteLine("Most Direct and Common way to create thread."); });  
  9.      }  
  10.  
  11.      #endregion  
2. Task using with Action
  1. #region Thread using Action  
  2.   
  3.       public void ThreadUsingAction()  
  4.       {  
  5.           Task task = new Task(new Action(PrintMessage));  
  6.           task.Start();  
  7.       }  
  8.   
  9.       private void PrintMessage()  
  10.       {  
  11.           Console.WriteLine("Thread using Action.");  
  12.       }  
  13.  
  14.       #endregion  
3. Task using Delegates
  1. #endregion  
  2.  
  3.     #region Thread using Delegate  
  4.   
  5.     public void ThreadUsingDelegate()  
  6.     {  
  7.         Task task = new Task(delegate { PrintMessageForDelegateThread(); });  
  8.         task.Start();  
  9.     }  
  10.   
  11.     private void PrintMessageForDelegateThread()  
  12.     {  
  13.         Console.WriteLine("Task Creation using Delegate");  
  14.     }  
  15.  
  16.     #endregion  
 4. Tasks using Lambda and named method:
  1. #region Thread using Lambda and named method  
  2.        public void ThreadUsingLambda()  
  3.        {  
  4.            Task task = new Task(() => PrintMessageMessageForLambda());  
  5.            task.Start();  
  6.        }  
  7.   
  8.        private void PrintMessageMessageForLambda()  
  9.        {  
  10.            Console.WriteLine("Task with Lambda");  
  11.        }  
  12.  
  13.        #endregion  
 5.  Task using Lambda and anonymous method:
  1. #region Lambda and anonymous method:  
  2.   
  3.      public void TaskUsingLambdaAndAnonymousMethod()  
  4.      {  
  5.          Task task = new Task(() => Console.WriteLine("Task with Anonymous Method"));  
  6.          task.Start();  
  7.      }  
  8.  
  9.      #endregion  
6. Creating and running Tasks Implicitly:
 
Tasks are created and maintained implicitly by the framework. It is used when developer needs to process Tasks which do not return any value and no more control is required.
 
The Parallel.Invoke method provides a best way to run any number of arbitrary statements concurrently. It creates Tasks implicitly.
  1. #region  Create Task implicitly using Parallel.Invoke  
  2.   
  3.     public void ImplicitTaskWithParallel()  
  4.     {  
  5.         Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());  
  6.     }  
  7.   
  8.     private void  DoSomeOtherWork()  
  9.     {  
  10.         Console.WriteLine("Execute second Tasks");  
  11.     }  
  12.   
  13.     private void  DoSomeWork()  
  14.     {  
  15.         Console.WriteLine("Execute First Task");  
  16.     }  
  17.  
  18.     #endregion  

Up Next
    Ebook Download
    View all
    Learn
    View all