1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| static void Main(string[] args)
{
const int ValueClientCalculations = 1000;
// One event is used for each ModelAccessor object
ManualResetEvent[] doneEvents = new ManualResetEvent[ValueClientCalculations];
ModelAccessor[] modelAccessorArray = new ModelAccessor[ValueClientCalculations];
// Configure and launch threads using ThreadPool:
Console.WriteLine("launching {0} tasks...", ValueClientCalculations);
for (int i = 0; i < ValueClientCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
ModelAccessor f = new ModelAccessor(i, doneEvents[i]);
modelAccessorArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
}
// Wait for all threads in pool to calculation...
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");
// Display the results...
for (int i = 0; i < ValueClientCalculations; i++)
{
ModelAccessor f = modelAccessorArray[i];
Console.WriteLine("ValueClient({0}) = {1}", f.N, f.valfin);
}
} |