using System.Threading;
public class Test
{
static void Main()
{
int taskCount = 4;
AutoResetEvent[] autoEvents = new AutoResetEvent[taskCount];
ThreadTask[] tasks = new ThreadTask[taskCount];
for (int i = 0; i < taskCount; i++)
{
autoEvents[i] = new AutoResetEvent(false);
tasks[i] = new ThreadTask(autoEvents[i]);
ThreadPool.QueueUserWorkItem(tasks[i].Calculation, new InputData(i+1,i+3));
}
for(int i=0;i
int index = WaitHandle.WaitAny(autoEvents);
if(index == WaitHandle.WaitTimeout)
{
Console.WriteLine("TimeOut!!");
}
else
{
Console.WriteLine("finished task for{0}, result:{1}", index,tasks[index].Result);
}
}
}
}
沒有留言:
張貼留言