多线程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!");
        }
    }
原文地址:https://www.cnblogs.com/shidengyun/p/5601719.html