多线程中 ManualResetEvent 的用法

        /// <summary>
        /// 手动重启
        /// </summary>
        private ManualResetEvent manualReset = new ManualResetEvent(false);
                if (suspend)
                {
                    manualReset.WaitOne();//暂停发送
                }
            SuspendCommand = new RelayCommand(o => { suspend = true; manualReset.Reset(); });
            RestoreCommand = new RelayCommand(o=> { suspend = false; manualReset.Set(); });

它可以通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源

 在多线程开发中,时常用到 ManualResetEvent 与 AutoResetEvent  。 它们如同道路交通中的信号灯。两者之间有什么区别呢?

共同点:

           均继承 EventWaitHandle 接口,因此,均具有以下功能:

           Reset() //红灯

           Set() //绿灯

           WaitOne() // 等待信号

不同点:

            AutoResetEvent   收到 Set 后 , 一次只能执行一个线程,其它线程继续 WaitOne 。

扫描二维码关注公众号,回复: 8429756 查看本文章

           ManualResetEvent  收到 Set 后,所有处理 WaitOne 状态线程均继续执行。

参考文章:https://www.cnblogs.com/howtrace/p/11362284.html  https://www.cnblogs.com/li-peng/p/3291306.html

猜你喜欢

转载自www.cnblogs.com/wgx0428/p/12153158.html