using System;
using System.Collections.Generic;
using System.Text;
using System.Threading ;namespace charpter18
{
public struct InputData
{
public int x;
public int y;
public InputData(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class ThreadTask
{
private AutoResetEvent autoEvent;
private int result;
public int Result
{
get
{
return result;
}
}
public ThreadTask(AutoResetEvent ev)
{
this.autoEvent = ev;
}
public void Calculation(object obj)
{
InputData data = (InputData)obj;
Console.WriteLine("Thread {0} starts calculation", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(new Random().Next(3000));
autoEvent.Set();
}
}
class Events1
{
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 < taskCount; 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);
}
}
}
}
}为什么有颜色的部分提示错误了 请各位帮忙看看 谢谢
using System.Collections.Generic;
using System.Text;
using System.Threading ;namespace charpter18
{
public struct InputData
{
public int x;
public int y;
public InputData(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class ThreadTask
{
private AutoResetEvent autoEvent;
private int result;
public int Result
{
get
{
return result;
}
}
public ThreadTask(AutoResetEvent ev)
{
this.autoEvent = ev;
}
public void Calculation(object obj)
{
InputData data = (InputData)obj;
Console.WriteLine("Thread {0} starts calculation", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(new Random().Next(3000));
autoEvent.Set();
}
}
class Events1
{
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 < taskCount; 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);
}
}
}
}
}为什么有颜色的部分提示错误了 请各位帮忙看看 谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货