我有一个简单的应用程序来演示CountdownEvent。这很好,但是我想以某种方式设置CountDownEvent的WaitHandle并使用它。有可能吗?如何做到这一点呢?我想我应该注册WaitHandle并将其传递给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();
}更新
我想用ManualResetEvent得到类似于示例的东西。无阻塞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...
}发布于 2017-12-05 20:48:12
您可以使用与ManualResetEvent相同的方法
RegisteredWaitHandle reg = ThreadPool.RegisterWaitForSingleObject
(_countDwn.WaitHandle, Go, "Some Data", -1, true);
/// ...
reg.Unregister(_countDwn.WaitHandle);https://stackoverflow.com/questions/47651316
复制相似问题