Thread 等待异步方法执行完成方案:AutoResetEvent.WaitOne()

Thread 等待异步方法执行完成方案:AutoResetEvent.WaitOne()

 

        static AutoResetEvent myresetevent = new AutoResetEvent(false);
        static int number = 0;
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(myfunc));
            thread.Name = "aaa";
            thread.Start();

            var time = 10;
            for (int i = 0; i < time; i++)
            {
                myresetevent.WaitOne();
                number++;
                Console.WriteLine("number:{0}", number);
            }
        }

        static void myfunc()
        {
            Thread.Sleep(3000);
            if(true)
            //while (true)
            {
                number += 10;
                Console.WriteLine("线程number:{0}", number);
                myresetevent.Set();
            }
        }

  

posted @ 2018-01-11 09:38  735882640  阅读(326)  评论(0编辑  收藏  举报