等待所有工作线程结束 [英] Wait for all worker threads to end

查看:168
本文介绍了等待所有工作线程结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
   C#线程/锁定混乱

我有以下问题:我有监控类,这是运行它自己的线程写入从排队到文件(所以主要的应用程序不必等待IO)。但是,当主应用程序线程结束(控制流运行之后它的最后一行),监视器线头也一样,即使它仍然运行(满的队列)。

I've got following problem: I have monitoring class, which is running it's own thread that writes from queue into file (so the main application doesn't have to wait on IO). But, when main application thread ends (control flow runs after it's last line), the monitor thread ends too, even if it is still running (full queue).

有没有什么办法,在不修改主线程等到工作线程做什么? C#。

Is there any way, without modifying main thread to wait till the worker thread is done? C#.

编辑:我不能修改主线程。我只写支持$ C $下巨大的应用与给定的API(一个静态方法里装的是应我写的,其中从配置读取),也没有办法如何改变线程,主要的应用程序不能依赖于我的code。

I cannot modify main thread. I'm writing only 'support' code for huge application with given API (one static method containing what shall I write, where is read from configuration), there is no way how to change threads, main app must not depend on my code.

推荐答案

您可以使用的ManualResetEvent并调用了WaitOne()在主要执行线程的结束。当工作线程做法仅仅是信号ManualResetEvent的,它会继续执行主线程的。

You can use a ManualResetEvent and call WaitOne() at the end of your main execution thread. When the worker thread is done simply signal the ManualResetEvent and it will continue execution of the main thread.

这篇关于等待所有工作线程结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆