【发布时间】:2010-02-18 07:22:27
【问题描述】:
我有一个主线程,还有许多其他后台线程。
这些后台线程的主要用途是查询数据(来自网络的许多查询,这就是我创建多个线程的原因:以避免用户界面滞后)。
在主线程(用户界面)中导出数据时,我需要等到所有其他线程都完成。
我的代码是:
//...code to open save file dialog...
//this loop is to wait for all the threads finish their query
//QueryThread.threadCount is the count of the background threads
while (QueryThread.threadCount != 0)
{
Thread.CurrentThread.Join(1000);
Console.WriteLine(QueryThread.threadCount);
}
//...code to export data...
如果我将while循环注释掉,程序会顺利运行,但我导出的一些数据可能会显示一些“不需要的”材料,因为一些后台线程还没有完成它们的工作。
但是,上面的while循环是无限的,threadCount永远不会改变,这意味着在“Join()”方法期间,没有后台线程在运行。
为什么后台线程被阻塞,我该如何解决这个问题?
非常感谢!
【问题讨论】:
标签: c# .net-2.0 multithreading