let's take a look at this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace pc.SignallingEventWaitHandleWithNumbers
{
class Program
{
static Random rnd = new Random(DateTime.Now.Millisecond);
static void Main(string[] args)
{
var tOdd = new Thread(Print);
var tEven = new Thread(Print);
tOdd.Start(Enumerable.Range(0, 20).Where(i => i % 2 ==
1));
tEven.Start(Enumerable.Range(0, 20).Where(i => i % 2 ==
0));
}
static void Print(object o) {
var nums = o as IEnumerable<int>;
foreach (var n in nums)
{
Thread.Sleep(rnd.Next(200, 700));
Console.Write($"{n} ");
}
}
}
}
by leveraging the EventWaitHandler we have the ability to send signals between threads
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace pc.SignallingEventWaitHandleWithNumbers
{
class Program
{
static Random rnd = new Random(DateTime.Now.Millisecond);
struct Data
{
public IEnumerable<int> Nums
{ get; private set; }
public EventWaitHandle SignalHandler { get; private set; }
public Data(IEnumerable<int>
Nums, EventWaitHandle SignalHandler)
{
this.Nums = Nums;
this.SignalHandler = SignalHandler;
}
}
static void Main(string[] args)
{
var tOdd = new Thread(Print);
var tEven = new Thread(Print);
//by setting
initialstate to true, the first signal handler wait
//does not need
to be signaled, it recieves a signal by default
using (var sh = new EventWaitHandle(true, EventResetMode.AutoReset))
{
tEven.Start(new Data(Enumerable.Range(0, 20).Where(i => i % 2 == 0), sh));
//enuser
that tEven starts first
Thread.Sleep(100);
tOdd.Start(new Data(Enumerable.Range(0, 20).Where(i => i % 2 == 1), sh));
//wait
for both threads to join method not to exit using before
//finished
with SignalHandler
tEven.Join();
tOdd.Join();
}
}
static void Print(object o)
{
var data = (Data)o;
foreach (var n in data.Nums)
{
//wait
for signal
data.SignalHandler.WaitOne();
Thread.Sleep(rnd.Next(200, 700));
Console.Write($"{n} ");
//send
signal
data.SignalHandler.Set();
}
}
}
}