Reputation: 18221
I have simple application that illustrates CountdownEvent
. It is fine, but I would like somehow to set WaitHandle
of CountDownEvent
and use it. Is it possible? How to achieve that? I suppose I should register WaitHandle
and pass it to CountDownEvent
?
public static CountdownEvent _countDwn = new CountdownEvent(3);
static void Main(string[] args)
{
new Thread(say).Start("hello 1");
new Thread(say).Start("hello 2");
new Thread(say).Start("hello 3");
_countDwn.Wait();
Console.WriteLine("done");
Console.ReadLine();
}
public static void Go(object data, bool timedOut)
{
Console.WriteLine("Started - " + data);
// Perform task...
}
public static void say(Object o)
{
Thread.Sleep(4000);
Console.WriteLine(o);
_countDwn.Signal();
}
UPD
I would like to get something similar to sample with ManualResetEvent
. No blocking wait()
:
static ManualResetEvent _starter = new ManualResetEvent (false);
public static void Main()
{
RegisteredWaitHandle reg = ThreadPool.RegisterWaitForSingleObject
(_starter, Go, "Some Data", -1, true);
Thread.Sleep (5000);
Console.WriteLine ("Signaling worker...");
_starter.Set();
Console.ReadLine();
reg.Unregister (_starter); // Clean up when we’re done.
}
public static void Go (object data, bool timedOut)
{
Console.WriteLine ("Started - " + data);
// Perform task...
}
Upvotes: 0
Views: 267
Reputation: 101603
You can use the same way you use for ManualResetEvent
:
RegisteredWaitHandle reg = ThreadPool.RegisterWaitForSingleObject
(_countDwn.WaitHandle, Go, "Some Data", -1, true);
/// ...
reg.Unregister(_countDwn.WaitHandle);
Upvotes: 0