Tuesday, May 5, 2009

Use ThreadPool to Queue Work Items

Example:

using System.Threading;

static void Main(string[] args)
{
WaitCallback callback=new WaitCallback(SomeMethod);
ThreadPool.QueueUserWorkItem(callback, "Hi");
ThreadPool.QueueUserWorkItem(callback, "Good Morning");
ThreadPool.QueueUserWorkItem(callback, "Good Bye");
}

static void SomeMethod(object state)
{
string greetingText=(string)state;
Console.Write(geetingText);
}

The step here is Use WaitCallBack delegate to call SomeMethod() and use ThreadPool static method to queue up serverl calls, parameter/object can be passed.

No comments: