I am looking at this site for threads. I have been playing with the code to answer the question "Does the CountdownEvent stop all threads?" the answer I got was no. Then I decided to play with the number that is passed into the CountdownEvent. Here is my code
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);
}
}
}
I am expecting that the thread that calls SaySomethingELse() to execute but the other threads execute as well even though only four signals have been called.
Why does it do that?
Thanks,
dhoehna
It looks to me like you've got
Signal
andWait
the wrong way round. If you want theSaySomething
calls to wait for the countdown to reach 0, you should be callingWait
. Here's an example:Output: