线程同步- EventWaitHandle类的子类ManualResetEvent的用法

来源:互联网 发布:网络的概念是什么 编辑:程序博客网 时间:2024/05/19 02:05

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        private ManualResetEvent goit = new ManualResetEvent(false);
        static void Main(string[] args)
        {

            Program ss = new Program();
            ss.Method1();
        }
        public void Method1()
        {
            Console.WriteLine("go!!!!!");

            goit.Reset();

            Thread thread = new Thread(new ThreadStart(Method2));
            thread.Start();

            goit.WaitOne();

            Method3();

            goit.Reset();
            goit.WaitOne();
            Method3();
        }
        public void Method2()
        {
            for (int i = 1; i < 20; i++)
            {
                if (i == 5)
                {
                    goit.Set();
                }
                if (i == 15)
                {
                    goit.Set();
                }
                Console.WriteLine(i.ToString());
                Thread.Sleep(900);
            }
        }
        public void Method3()
        {
            for (int i = 1; i < 5; i++)
            {
                Console.WriteLine("ok!");
                Thread.Sleep(1000);

            }
        }
    }
}

 

原创粉丝点击