Skip to main content

Task Parallel Library


Problem: One of the clients was running the multi threaded application on a single core machine, he upgraded his system to Quadra-core and he observed that the application did not improve or started degrading the performance. The development team later found the reason that applications were not utilizing the benefits of cores and it was still running in single core. Solution: .Net 4.0 has brought a new Task Parallel library which uses the benefits of multi core. The Library efficiently utilizes the system resources to give better performance. Internally it uses Threadpool with improved algorithm with .Net 4.0

NamespaceSystem.Threading.
Tasks Class - Task

Create Task
Task t1 = new Task(() => Add(1,100));
t1.Start();
t1.Wait(); //Wait till task t1 is completed and then starts t2 Task
t2 = new Task(() => Add(101, 200));
t2.Start();

//Other way of creating multiple task
var tasks = new Task[2];
tasks[0] = Task.Factory.StartNew(() => Add(1, 100));
tasks[1] = Task.Factory.StartNew(() => Add(101, 200));
Task.WaitAll(tasks);

Create Dependency of Task
//Add and then display no’s and see output without creating Dependency
var tasks1 = new Task[4];
tasks1[0] =Task.Factory.StartNew(() => Add(1, 50));
tasks1[1] =Task.Factory.StartNew(() => Add(101, 150));
tasks1[2] =Task.Factory.StartNew(() => Display());
tasks1[3] =Task.Factory.StartNew(() => Display());

//Add and then display no’s and create dependency for sequential display
var tasks1 = new Task[4];
tasks1[0] =Task.Factory.StartNew(() => Add(1, 50));
tasks1[1] =Task.Factory.StartNew(() => Add(101, 150));
// start after completing adding of tasks1
tasks1[2] =tasks1[0].ContinueWith((t) => Display());
// start after completing tasks1[2]
tasks1[3] =tasks1[2].ContinueWith((t) => Display());

Cancel Running Task
For Cancelling a task following steps need to be performed:

1. Create Method with input parameter of Cancellation token and check for IsCancellationRequested Property of token to display the message that task is cancelled

public static void CancelProcess(CancellationToken ct)
{
      Thread.Sleep(1000);
      if (ct.IsCancellationRequested)
     {
         Console.WriteLine("Process cancelled");
     }
}

2. Create token:

      var source = new CancellationTokenSource();
      var token = source.Token;

3. Create task and call the CancelProcess method, also pass the token to the method
      var taskCancel = Task.Factory.StartNew(() => CancelProcess(token), token);

4. Cancel the token source on User's request:
      source.Cancel();

Comments

Popular posts from this blog

C# Generic Factory

Implement Factory pattern using generics     public interface IDoWork   {       string DoWork();   }     Declare an Interface first by abstracting the common  functionality    Here I am taking the example of DoWork     public class Manager : IDoWork   {     public string DoWork()     {         return "Manager Manages School" ;     }   }     Implement the IDoWork in concrete classes as shown      public class Teacher : IDoWork     {         public string DoWork()         {             return "Teacher teaches student in school" ;         ...

How to enable windows authentication in PostgreSQL

1.     Steps to create user or role in PostgreSQL ·         Open pgAdmin III ·         Login to PostgreSQL database ·         Select “Login Roles” and right click on mouse to view the context menu ·         Select “New Login Role” from context menu ·         Enter desired user name in “Role name” text box ·         Select “Definition” tab in “New Login Role” window ·         Enter desired Password in the given text box ·         Select “Role privileges” tab in “New Login Role” window ·         Select the privileges for the entered user or role ·         Select “SQL”...

UML - Association, Aggregation, Composition, Generalization, Specialization, Realization and Dependency

Association Association is a simple relationship between two classes. For example A relationship between Professor Class and Student class is known as Association. Both Classes can exist without each other, so Professor and Student are two independent classes. In this kind of relationships there will not be any owner class. Both classes have their own life cycle. UML Notation:     Aggregation Aggregation is a special type of Association. It is known as “Has-A” relationship. For example A Department class can contain Professor Class. Here Department class is owner class. Here in this relationship even after deleting Department class, Professor Class can exits. UML Notation: Composition Composition is a special type of Aggregation. It is known as “Is-A” relationship. For example A University Class has many Department class. Here University and Department objects are dependent on each other. If we delete Univ...