多线程11-AutoResetEvent
Posted shidengyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程11-AutoResetEvent相关的知识,希望对你有一定的参考价值。
class Program
{
private static AutoResetEvent workEvent = new AutoResetEvent(false);
private static AutoResetEvent mainEvnet = new AutoResetEvent(false);
static void Process(int second)
{
Console.WriteLine("a long run");
Thread.Sleep(second);
Console.WriteLine("work is done");
workEvent.Set();
Console.WriteLine("Wait for a main Thread to complete the work");
mainEvnet.WaitOne();
Console.WriteLine("starting second opeartion...");
Thread.Sleep(5000);
Console.WriteLine("Work is done");
workEvent.Set();
}
static void Main()
{
var t = new Thread(() => Process(10));
t.Start();
Console.WriteLine("wait for another thread to complete work");
workEvent.WaitOne();
Console.WriteLine("First Operation is Complete");
Console.WriteLine("Peffoming an operation on a mian thread");
Thread.Sleep(TimeSpan.FromSeconds(5));
mainEvnet.Set();
Console.WriteLine("Now Runing the second Opeartion on an second thread");
workEvent.WaitOne();
Console.WriteLine("second opeartion is completed!");
}
}
{
private static AutoResetEvent workEvent = new AutoResetEvent(false);
private static AutoResetEvent mainEvnet = new AutoResetEvent(false);
static void Process(int second)
{
Console.WriteLine("a long run");
Thread.Sleep(second);
Console.WriteLine("work is done");
workEvent.Set();
Console.WriteLine("Wait for a main Thread to complete the work");
mainEvnet.WaitOne();
Console.WriteLine("starting second opeartion...");
Thread.Sleep(5000);
Console.WriteLine("Work is done");
workEvent.Set();
}
static void Main()
{
var t = new Thread(() => Process(10));
t.Start();
Console.WriteLine("wait for another thread to complete work");
workEvent.WaitOne();
Console.WriteLine("First Operation is Complete");
Console.WriteLine("Peffoming an operation on a mian thread");
Thread.Sleep(TimeSpan.FromSeconds(5));
mainEvnet.Set();
Console.WriteLine("Now Runing the second Opeartion on an second thread");
workEvent.WaitOne();
Console.WriteLine("second opeartion is completed!");
}
}
以上是关于多线程11-AutoResetEvent的主要内容,如果未能解决你的问题,请参考以下文章