Powered by Blogger.

Saturday, March 1, 2014

How to create Thread Pool via TPL in C#.Net

In this post we will discuss how to create Thread Pool via TPL in C#.Net.

Also check out:

- Get nth highest lowest salary in SQL Server 2008

- How to validate user input to accept only 10 character using Regulat expressions in Javascript?

- Populate dataset with data adapter and XML in Asp.Net

The task parallel library(TPL) provides task class to enter into thread pool easy. The task class is the part of .Net Framework 4.0 . The nongeneric Task class is a replacement for ThreadPool.QueueUserWorkItem, and the generic Task<TResult> is a replacement for asynchronous delegates. The newer constructs are faster, more convenient, and more flexible than the old.

To use the nongeneric Task class, call Task.Factory.StartNew, passing in a delegate of the target method:

using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System;
class tpl_threadpool
    static void Display()
        Console.WriteLine("Example for thread pool created by TPL...");
    static void Main() // The Task class is in System.Threading.Tasks