c# 怎么等待线程池中所有线程都运行结束在运行主线程
主线程执行。。。线程1加入线程池线程2加入线程池线程3加入线程池线程4加入线程池...主线程等待线程池中加入的线程运行完毕。。。主线程执行。。。。怎么实现上面的步骤?如果...
主线程执行
。。。
线程1加入线程池
线程2加入线程池
线程3加入线程池
线程4加入线程池...
主线程等待线程池中加入的线程运行完毕。。。
主线程执行
。。。。
怎么实现上面的步骤?如果线程池只加入一个,那么在线程运行结束调用manulresetevent类的set()方法即可,但是加入一个以上该怎么办呢? 展开
。。。
线程1加入线程池
线程2加入线程池
线程3加入线程池
线程4加入线程池...
主线程等待线程池中加入的线程运行完毕。。。
主线程执行
。。。。
怎么实现上面的步骤?如果线程池只加入一个,那么在线程运行结束调用manulresetevent类的set()方法即可,但是加入一个以上该怎么办呢? 展开
2个回答
展开全部
可以参见 WaitHandle.WaitAll 方法。
WaitHandle的命名空间是System.Thread。
可以为每一个线程创建一个
WaitHandle,比如:
WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false),
...
};
然后在每条线程结束时,set与之相对应的那个 WaitHandle,
在主线程中,使用
WaitHandle.WaitAll方法来等待所有的WaitHandle,
比如:
WaitHandle.WaitAll(waitHandles);
以下是MSDN中的示例代码:
using System;
using System.Threading;
public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
// Define a random number generator for testing.
static Random r = new Random();
static void Main()
{
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);
// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);
// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}
static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}
// This code produces output similar to the following:
//
// Main thread is waiting for BOTH tasks to complete.
// Performing a task for 7000 milliseconds.
// Performing a task for 4000 milliseconds.
// Both tasks are completed (time waited=7064.8052)
//
// The main thread is waiting for either task to complete.
// Performing a task for 2000 milliseconds.
// Performing a task for 2000 milliseconds.
// Task 1 finished first (time waited=2000.6528).
WaitHandle的命名空间是System.Thread。
可以为每一个线程创建一个
WaitHandle,比如:
WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false),
...
};
然后在每条线程结束时,set与之相对应的那个 WaitHandle,
在主线程中,使用
WaitHandle.WaitAll方法来等待所有的WaitHandle,
比如:
WaitHandle.WaitAll(waitHandles);
以下是MSDN中的示例代码:
using System;
using System.Threading;
public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
// Define a random number generator for testing.
static Random r = new Random();
static void Main()
{
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);
// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);
// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}
static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}
// This code produces output similar to the following:
//
// Main thread is waiting for BOTH tasks to complete.
// Performing a task for 7000 milliseconds.
// Performing a task for 4000 milliseconds.
// Both tasks are completed (time waited=7064.8052)
//
// The main thread is waiting for either task to complete.
// Performing a task for 2000 milliseconds.
// Performing a task for 2000 milliseconds.
// Task 1 finished first (time waited=2000.6528).
参考资料: MSDN
展开全部
您能自己在msdn上先看看thread的相关方法和资料吗。。。。这都是太基础的问题了。。。。
http://www.microsoft.com/china/msdn/
http://msdn.microsoft.com/zh-cn/library/default.aspx
http://www.microsoft.com/china/msdn/
http://msdn.microsoft.com/zh-cn/library/default.aspx
参考资料: http://msdn.microsoft.com/zh-cn/library/default.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询