Search 
DailyCoding > General

Using Thread Pool for Multi-Threading application

How to use thread pool for multithreading in a .net application
Author admin on Jun 9, 2008 0 Comments
Rate it    (Rated 5 by 1 people)
14,486 Views

Thread pool is basically collection of threads which can be used for preforming task in background. Using thread pool is a good practice over creating a new thread for you task. Whenever the task of a thread from thread pool is completed, then the thread is automatically returned back into the thread queue so that it can be re-used by other asynchronous requests. .NET provides inbuilt support for thread pool. Below is a simple example which will explain how can we use the thread pool.

using System;
using System.Threading;

static void Main()
{
  // Calling MyWaitCallback asynchronously
  ThreadPool.QueueUserWorkItem(MyWaitCallback, 0);
  Console.WriteLine("Please wait while we are processing...");
}

public static void MyWaitCallback(object state)
{
  int threadNo = (int)state;
  
  // Processing code here
  
  Console.WriteLine("Process Completed Thread {0}...", threadNo);
}
C# | Utility | Windows

Discussion

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved