问题描述
我有一个发射了线程做一些工作的方法。将有一段时间运行的异步2个线程,而当他们的回调方法获取的调用,关闭另一个线程回调火灾,直到所有的工作已经完成。如何让我的方法等待这些线程完成和被解雇了吗?
I have a method that is firing off threads to do some work. There will be 2 threads running asynchronously for a period of time, and when their callback method get's called, the callback fires off another thread until all the work has been finished. How do I make my method wait for all these threads to finish and be fired off?
推荐答案
Interlocked.Increment刚开始任何线程之前最初零计数器。 Interlocked.Decrement在短短的出口/回环前每一个线程的计数器。如果任何线程递减计数器清零,集()的的AutoResetEvent。 WaitOne的()上的AutoResetEvent。
Interlocked.Increment an initially zeroed counter just before starting any thread. Interlocked.Decrement a counter in every thread just before exit/loopback. If any thread decrements the counter to zero, Set() an AutoResetEvent. WaitOne() on the AutoResetEvent.
RGDS,
马丁
Rgds,Martin
这篇关于我如何让我的方法等待所有线程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!