日期:2014-05-17 浏览次数:20823 次
public partial class Form1 : Form
{
int count = 8;
object obj = new object();
Thread thread;
ManualResetEvent[] events;
List<TaskInfo> tasks;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
txtMsg.Text = "";
thread = new Thread(new ThreadStart(Test));
thread.Start();
}
public void Test()
{
events = new ManualResetEvent[count];
tasks = Init();
for (int i = 0; i < count; i++)
{
events[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), i);
}
ManualResetEvent.WaitAll(events);
InvokeSetText("任务完成");
}
public void DoWork(object o)
{
int i = (int)o;
TaskInfo model;
while (tasks.Count > 0)
{
Monitor.Enter(obj);
try
{
model = tasks[0];