Logo 
Search:

.Net Framework FAQ

Submit Interview FAQ
Home » Interview FAQ » .Net FrameworkRSS Feeds

How do I use the thread pool?

  Shared By: Agatha Miller    Date: Jan 16    Category: .Net Framework    Views: 1088

Answer:

By passing an instance of a WaitCallback delegate to the ThreadPool.QueueUserWorkItem() method

class CApp
{
static void Main()
{
string s = "Hello, World";
ThreadPool.QueueUserWorkItem( new WaitCallback( DoWork ), s );

Thread.Sleep( 1000 ); // Give time for work item to be executed
}

// DoWork is executed on a thread from the thread pool.
static void DoWork( object state )
{
Console.WriteLine( state );
}
}

Share: 
 

Didn't find what you were looking for? Find more on How do I use the thread pool? Or get search suggestion and latest updates.


Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: