- CountDownEvent which we'll cover in this post
- EventWaitHandle which we'll look at in the next post
using System;
using System.Threading;
namespace pc.ThreadSignalling
{
class Program
{
struct Data {
public int Delay { get; set; }
public string Value { get; set; }
public Data(int Delay, string Value) {
this.Delay = Delay;
this.Value = Value;
}
}
static void Main(string[] args)
{
var threads = new Thread[6];
var data = "Hello world my name is
Pawel".Split(' ');
for (int i = 0,
delay = 500; i < threads.Length; i++, delay += 500)
{
threads[i] = new Thread(Print);
threads[i].Start(new Data(delay, data[i]));
}
//wait for each
thread to finish before continuing with the main
foreach (var t in threads)
t.Join();
Console.WriteLine("Woot Woot");
}
static void Print(object o) {
var data = (Data)o;
Thread.Sleep(data.Delay);
Console.WriteLine(data.Value + " ");
}
}
}
using System;
using System.Threading;
namespace pc.ThreadSignalling
{
class Program
{
public struct Data
{
public int Delay { get; set; }
public string Value { get; set; }
public CountdownEvent cdEvent { get; set; }
public Data(int Delay, string Value, CountdownEvent cdEvent)
{
this.Delay = Delay;
this.Value = Value;
this.cdEvent = cdEvent;
}
}
static void Main(string[] args)
{
using (var cdEvent
= new CountdownEvent(6))
{
var threads = new Thread[6];
var data = "Hello world my name is
Pawel".Split(' ');
for (int i = 0,
delay = 500; i < threads.Length; i++, delay += 500)
{
threads[i] = new Thread(Print);
threads[i].Start(new Data(delay, data[i], cdEvent));
}
//Wait for 6
signals before continuing
cdEvent.Wait();
}
Console.WriteLine("WOot woot");
}
static void Print(object o)
{
var data = (Data)o;
Thread.Sleep(data.Delay);
Console.Write(data.Value + " ");
data.cdEvent.Signal();
}
}
}