Я выполнил приведенную ниже программу пула потоков, но проблема в том, что WaitCallBackMethod (здесь ThreadPoolCallback) вызывается 2 раза (который в идеале должен называться 1ce). какую ошибку я совершаю?
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
}
Если я делаю
//Initializang all the manual reset events first
Enumerable.Range(0, exposureManualResetEvent.Length).ToList().ForEach(i =>
{
exposureManualResetEvent[i] = new ManualResetEvent(false);
});
Enumerable.Range(0, 1).ToList().ForEach(i =>
{
ThreadPool.QueueUserWorkItem(ExposureThreadPoolCallback, i);
});
Программа зависает!
Я использую C # 3.0
Спасибо