如何在主程序中等待新建的线程执行完毕后再继续运行? 例如 主程序调用了两个线程a和b我想让主程序等到a,b都执行完毕后再 往下运行应该怎么做?最好能有示例代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你在主线程中加上public static void Main(){ a.Join(); b.Join(); a.Start(); b.Start(); //下面加上你的代码}试试 Thread.Join().using System;using System.Threading;class IsThreadPool{ static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread = new Thread(new ThreadStart(ThreadMethod)); regularThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent); // Wait for foreground thread to end. regularThread.Join(); // Wait for background thread to end. autoEvent.WaitOne(); } static void ThreadMethod() { Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.", Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); } static void WorkMethod(object stateInfo) { Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.", Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished. ((AutoResetEvent)stateInfo).Set(); }}参见:Thread.Join Method ms-help://MS.MSDNQTR.v90.en/fxref_mscorlib/html/56ed7b6f-efe0-67e7-34bc-766dd9f693f9.htm ms-help://MS.MSDNQTR.v90.en/fxref_mscorlib/html/56ed7b6f-efe0-67e7-34bc-766dd9f693f9.htm有在线的帮助吗? 如果我是往线程池里加入了很多WaitCallBack 如何使线程池中的线程都执行完毕前阻塞主线程呢? 谢谢 倒计时页面跳转问题 winform下如何吧个人简历导出word? 哟西,,小日本死啦死啦地,怎样做一个东东让CS的界面切换中英文 请教:c#-用API获取已知IE窗口的IHTMLDocument2,问题见代码 因缺少 CategoryName 而未能初始化 这条语句什么意思 @@@@@@@@@@@@@@ dotNet中怎行实现 BitBlt 功能 webservice异步调用问题? 有什么.net打包工具吗? 将我刚才100分的垃圾回收讨论帖的问题具体话,再讨论下。。(最后38分,不要只看分,进来讨论学习一下) c# tChart 图表控件绑定数据 给代码的100分。
{
a.Join();
b.Join(); a.Start();
b.Start(); //下面加上你的代码
}
试试
using System.Threading;class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread =
new Thread(new ThreadStart(ThreadMethod));
regularThread.Start();
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),
autoEvent); // Wait for foreground thread to end.
regularThread.Join(); // Wait for background thread to end.
autoEvent.WaitOne();
} static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
} static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}参见:
Thread.Join Method
ms-help://MS.MSDNQTR.v90.en/fxref_mscorlib/html/56ed7b6f-efe0-67e7-34bc-766dd9f693f9.htm