Problem in thread pool implementation(C#3.0)
- by Newbie
Hi Experts,
I have done the below thread pool program but the problem is that the WaitCallBackMethod(here ThreadPoolCallback) is getting called 2 times(which ideally should be called 1ce). what is the misktake I am making?
public class Calculation
{
#region Private variable declaration
ManualResetEvent[] factorManualResetEvent = null;
#endregion
public void Compute()
{
factorManualResetEvent = new ManualResetEvent[2];
for (int i = 0; i < 2; i++){
factorManualResetEvent[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, i);}
//Wait for all the threads to complete
WaitHandle.WaitAll(factorManualResetEvent);
//Proceed with the next task(s)
NEXT_TASK_TO_BE_EXECUTED();
}
#region Private Methods
// Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Method1();
Method2();
factorManualResetEvent[threadIndex].Set();
}
private void Method1 ()
{ //Code of method 1}
private void Method2 ()
{ //Code of method 2 }
#endregion
}
I am using C#3.0
Thanks