在win表单应用程序中,我有一个如下所示的线程数组:
bool stop = false;
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
threads[i] = new Thread(new ThreadStart(Job));
// How to make sure all threads have exited, when the boolean = false
void Job()
{
while (!stop)
// Do something
}
现在,如果用户按下STOP,stop
的布尔值将设置为true,因此线程会一个接一个地退出Job
方法。如何确保退出所有线程?
注意:我的情况需要传统的线程,TaskLibrary
不符合我的情况。
答案 0 :(得分:7)
使用Join方法检查所有线程是否已停止。
foreach (var t in threads)
{
t.Join();
}
答案 1 :(得分:4)
您是否考虑过使用BackgroundWorkers?你说“传统线程”..我不完全确定你的意思,所以我不知道这是否是一个有效的提案,但这里无论如何Join()
无法解决你的问题
BackgroundWorker[] workers = new BackgroundWorker[10];
bool allThreadsDone = false;
// initialize BackgroundWorkers
for (int i = 0; i < 10; i++)
{
workers[i] = new BackgroundWorker();
workers[i].WorkerSupportsCancellation = true;
workers[i].RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
workers[i].DoWork += new DoWorkEventHandler(AlgorithmsUI_DoWork);
workers[i].RunWorkerAsync();
}
// thread entry point..DoWork is fired when RunWorkerAsync is called
void AlgorithmsUI_DoWork(object sender, DoWorkEventArgs e)
{
while (!stop)
// do something
}
// this event is fired when the BGW finishes execution
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bool threadsStillRunning = false;
foreach (BackgroundWorker worker in workers)
{
if (worker.IsBusy)
{
threadsStillRunning = true;
break;
}
}
if (!threadsStillRunning)
allThreadsDone = true;
}
protected override OnFormClosing(FormClosingEventArgs e)
{
if (!allThreadsDone)
{
e.Cancel = true;
MessageaBox.Show("Threads still running!");
}
}
如果任何线程仍在运行,这应该可以防止表单关闭。
答案 2 :(得分:1)
我不确定这是否是您正在寻找的,但这是我在.NET 3.0中使用的一个简单的解决方案,以确保在继续之前完成了大量但确定的线程数:
全局:
AutoResetEvent threadPoolComplete = new AutoResetEvent(false);
static int numThreadsToRun;
激活线程时:
numThreadsToRun = [number of threads];
[start your threads];
threadPoolComplete.WaitOne();
在每个线程代码的末尾:
if (Interlocked.Decrement(ref numThreadsToRun) == 0)
{
threadPoolComplete.Set();
}