标签: countdownevent

CountdownEvent返回零

我试图使用CountdownEvent只允许线程在事件的计数为零时继续,但我希望初始计数为零.实际上,我希望返回到零行为,每当计数为零时,就会发出事件信号,并且只要线程大于零,就会使线程等待.

我可以使用0初始计数初始化Countdown事件,但是当我尝试添加到计数时,我得到InvalidOperationException "CountdownEvent_Increment_AlreadyZero".

是否有替代类或其他方式我可以使用倒计时事件以避免此限制?

.net c# multithreading countdownevent

13
推荐指数
2
解决办法
4800
查看次数

CountDownEvent不会等到所有信号都被调用

我正在寻找这个网站的线程.我一直在玩代码来回答"CountdownEvent是否会阻止所有线程?" 我得到的答案是否定的.然后我决定使用传递给CountdownEvent的数字.这是我的代码

namespace ThreadPractice
{
    class Program
    {
        static CountdownEvent CountDown = new CountdownEvent(4);
        static void Main()
        {
            new Thread(() => SaySomething("I am Thread one.")).Start();
            new Thread(() => SaySomething("I am thread two.")).Start();
            new Thread(() => SaySomethingElse("Hello From a different Thread")).Start();
            new Thread(() => SaySomething("I am Thread Three.")).Start();
            CountDown.Wait();
            Console.Read();
        }

        static void SaySomething(string Something)
        {
            Thread.Sleep(1000);
            Console.WriteLine(Something);
            CountDown.Signal();
        }

        static void SaySomethingElse(string SomethingElse)
        {
            Thread.Sleep(1000);
            Console.WriteLine(SomethingElse);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我期待调用SaySomethingELse()的线程执行但其他线程也执行,即使只调用了四个信号.

为什么这样做?

谢谢,

dhoehna

c# multithreading countdownevent

1
推荐指数
1
解决办法
4906
查看次数

标签 统计

c# ×2

countdownevent ×2

multithreading ×2

.net ×1