Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> ThreadPool in C# Programming
ThreadPool in C# Programming
ThreadPool in C# Programming


by Arun Singh on 8/26/2011 8:10:33 PM

Views: 14482       Comments: 0

ThreadPool in C# Programming

ThreadPool is an efficient way through which we can process jobs in parallel in C# programming language. ThreadPool is a collection of threads that can be used to perform many tasks in background i.e. we can say that ThreadPool Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O and wait on behalf of other threads etc.

Thread pools are often employed in server applications. Each incoming request is assigned to a thread from the thread pool, so the request can be processed asynchronously, without tying up the primary thread or delaying the processing of subsequent requests.

Thread pools typically have a maximum number of threads. If all the threads are busy, additional tasks are placed in queue until they can be serviced as threads become available.

Here I am giving an example of ThreadPool class.  Let’s see the brief demonstration of example.

Example:

using System.Threading;

using System.Diagnostics;

 

namespace PoolThread

{

    class MyProcess

    {

        public ManualResetEvent doneEvent;

 

        public MyProcess(ManualResetEvent sendEvent)

         {

        // Assign notify event from ThreadPool

         this.doneEvent = sendEvent;

         }

 

       public void MyProcessThreadPoolCallback(Object index)

        {

            int threadIndex = (int)index;

         Console.WriteLine("thread {0} started...", threadIndex);

        // Call the process that is created on every thread

         StartProcess();

         Console.WriteLine("thread {0} end...", threadIndex);

 

        // Indicates that the process has been completed

         this.doneEvent.Set();

        }

 

// Start any process

    public void StartProcess()

     {

        // Start process that open c:\ directory

         Process.Start("C:\\");

      }

  

 }

 

public class ThreadPoolExample

 {

     static void Main()

     {

         const int totalCountProcess = 10;

         // Create manualresetevent array to assign with process

         ManualResetEvent[] sendEvents = new ManualResetEvent[totalCountProcess];

         // Create MyProcess objects array

         MyProcess[] MyProcessArray = new MyProcess[totalCountProcess];

      

         // Configure and launch threads using ThreadPool:

         Console.WriteLine("launching thread...");

         for (int i = 0; i < totalCountProcess ; i++)

         {

             sendEvents[i] = new ManualResetEvent(false);

             MyProcess p = new MyProcess(sendEvents[i]);

             MyProcessArray[i] = p;

             // ThreadPool call QueueUserWorkItem method to execute when ThreadPool having thread

             ThreadPool.QueueUserWorkItem(p.MyProcessThreadPoolCallback, i);

             // After execute one thread it go in sleep mode for 2 second

             Thread.Sleep(2000);

         }

         // Wait for all thread to complete execution

         WaitHandle.WaitAll(sendEvents);

         Console.WriteLine("All process are complete.");

         Console.ReadKey();

 

           }

 

    }

}

Desired Output:

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 7464
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.