In this post we will see a simple code example which illustrates how to use the ThreadPool to queue up tasks in a multi-threaded environment. We first need to create an object that contains information required for the task. This object will be passed as parameter to a task that is queued for execution by the thread pool.
private void QueueWork(string parameter)
{
// Instantiate an object containing the information required for the task
object[] obj = new object[3];
obj[0] = "Work 1";
obj[0] = "Work 2";
obj[0] = "Work 3";
//Queue the task and pass required data as paramaeter
ThreadPool.QueueUserWorkItem(new System.Threading.ThreadPool.QueueUserWorkItem(DoWork) , obj);
}
// The thread performs the independent task
private void DoWork(object workItem)
{
object[] workList =(object[])workItem;
// do the work as per details in workList
}
0 Comment(s)