如何使用Event事件对异步线程进行阻塞和放行?

发布时间 2023-11-04 15:21:24作者: 尼古拉-卡什

//定义信号事件

static AutoResetEvent autoResetEvent = new AutoResetEvent(false);

//定义要异步执行的方法

static void A()
    {
        for (int i = 0; i < 10; i++)
        {
            autoResetEvent.WaitOne();//阻塞 等待信号
            Console.Write("A");
        }
    }
 
//调用异步方法
new Action(A).BeginInvoke(null, null);
 
//放行
autoResetEvent.Set();