Task Parallel Library (TPL) – Part 1

Different ways to start Task Parallel Library (TPL) Thread(s)

1:  using System.Threading.Tasks;  
1:  public void Method()  
2:  {  
3:  }  
1:  // Using Task.Factory.StartNew  
2:    
3:  Task.Factory.StartNew(Method); // Method Group  
4:  Task.Factory.StartNew(new Action(Method)); // Action Delegate  
5:  Task.Factory.StartNew(() => Method()); // Lambda Expression  
6:  Task.Factory.StartNew(delegate { Method(); }); // Delegate  
7:    
8:  // Using Task.Run()  
9:    
10:  Task.Run(new Action(Method));  
11:  Task.Run(() => Method());  
12:  Task.Run(delegate { Method(); });  
13:    
14:  // Using Task object  
15:    
16:  Task task1 = new Task(Method);  
17:  task1.Start();  
18:    
19:  Task task2 = new Task(new Action(Method));  
20:  task2.Start();  
21:    
22:  Task task3 = new Task(() => Method());  
23:  task3.Start();  
24:    
25:  Task task4 = new Task(delegate { Method(); });  
26:  task4.Start();  

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s