Some Key wrods:

  • An object containing information to be used by the callback method.
    WaitCallback represents a callback method that you want to execute on a ThreadPool thread. Create the delegate by passing your callback method to the WaitCallback constructor. Your method must have the signature shown here.

     

using System;
using System.Threading;
//csc thread_pool_sleep.cs
namespace ex_thread_pool
{
public class thread_pool {
Object obj = new Object();
public static void thread_process() {
// Queue the task.
Console.WriteLine("----Call Back Region---");
Console.WriteLine("Write a slogan for your voters " );
string str = Console.ReadLine();
Thread.CurrentThread.Name ="main";
//obj = Thread.CurrentThread.ApartmentState.ToString();
string cur = Thread.CurrentThread.Name.ToString();
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
Console.WriteLine("Main thread does some work, then sleeps.");
Console.WriteLine("Main,Thread, " + "is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
// If you comment out the Sleep, the main thread many instance it would be you r # 1
//exits before the thread pool task runs. The thread pool uses background
// threads, which do not keep the application running. You would note
Thread.Sleep(3000);

Console.WriteLine("Back to Thread no : {0}",Thread.CurrentThread.GetHashCode().ToString());
Console.WriteLine("Thanks for the wait, I need to wait for a feed back using sleep " );
Console.WriteLine();
Console.WriteLine("----No Call Back Region---");
thread_pool tp = new thread_pool();
tp.do_something(str);
Console.WriteLine("Back to Thread {0} ", Thread.CurrentThread.GetHashCode().ToString());
// Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");

}
// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Console.WriteLine("\tIn--- thread pool.");
Console.Write("\tThread no : {0}",Thread.CurrentThread.GetHashCode().ToString());
Console.WriteLine(" is a worker t looks OK " +
" and {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");

Console.WriteLine("\tOut--- thread pool.");
}
private void do_something(string str) { Thread t1 = new Thread(voter_in);
Console.WriteLine("Opening new thread got. {0} ", str);
t1.Name ="voter";
t1.Start();
Console.WriteLine("-- do_something, " + " and {0} from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
t1.Join();
}
public static void voter_in(){
//int n1 = 20, n2 = 4;
Console.WriteLine("\t\tThread no : {0}",Thread.CurrentThread.GetHashCode().ToString());
Console.WriteLine("\t\tThread name : {0}",Thread.CurrentThread.Name.ToString()); Console.WriteLine("\t-- {0} ",DateTime.Now);
}
}
class test {
static void Main() {
thread_pool.thread_process();
Console.ReadLine();
}
}
}
 

 
There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:
  • You require a foreground thread.
  • You require a thread to have a particular priority.
  • You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.
  • You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.
  • You need to have a stable identity associated with the thread, or to dedicate a thread to a task.

 

As described before that "Thread.Sleep(MS)", suspend the main thread and allow other thread to work during the sleep. I used sharp developer 2.0 to show that when you remark

It ran obviously different, the thread #3 did not form within call-back region and moved to "NoCall back region. Note the line" Thanks for the wait ...........", appeared early.

 
The contrasts are shown below.