chi*_*tom 13 .net c# multithreading countdownevent
我试图使用CountdownEvent只允许线程在事件的计数为零时继续,但我希望初始计数为零.实际上,我希望返回到零行为,每当计数为零时,就会发出事件信号,并且只要线程大于零,就会使线程等待.
我可以使用0初始计数初始化Countdown事件,但是当我尝试添加到计数时,我得到InvalidOperationException "CountdownEvent_Increment_AlreadyZero".
是否有替代类或其他方式我可以使用倒计时事件以避免此限制?
你写了:
我正在执行一个操作,将创建未知数量的子操作(不是任务或线程)
那他们是什么?你应该做这样的事情:
CountdownEvent ev; public void foo() { ev = new CountdownEvent(1); foreach ( <task in tasks_to_start> ) { ev.AddCount(); // enter code here which starts your task } ev.Signal(); ev.Wait(); } public static void youtTask(CountdownEvent ev) { // some work // ... // after all is done ev.Signal(); }