AutoResetEvent控制线程用法 本文主要来自一道面试题,由于之前对AutoResetEvent的概念比较模糊(即使已经使用过了)。面试题题目很简洁:两个线程交替打印0~100的奇偶数。 AutoResetEvent概念 AutoResetEvent对象用来进行线程同步操作,AutoResetEvent类继承waitHandle类。 -》可以简单理解如果AutoResetEvent对象是终止状态,就像不管别人了,任你撒野去(waitOne()得到的都是撒野信号) AutoResetEvent顾名思义,其对象在调用一次set之后会自动调用一次 AutoResetEvent主要方法有 AutoResetEvent(bool initialState):构造函数,用一个指示是否将初始状态设置为终止的布尔值初始化该类的新实例。 = new AutoResetEvent(false); static AutoResetEvent evenResetEvent = new AutoResetEvent(false
AutoResetEvent一般用来从一个线程向另一个线程发送通知。 下面我用两个线程来使用下AutoResetEvent private static AutoResetEvent _workerEvent = new AutoResetEvent(false workerEvent 发送信号"); _workerEvent.Set(); Console.ReadKey(); } } 在初始时将AutoResetEvent 的值设为false,在AutoResetEvent中会存着一个bool值,false时线程阻塞,true线程退出阻塞。
三、AutoResetEvent和ManualResetEvent的区别 既然AutoResetEvent和ManualResetEvent都是收费站,那么它们之间有什么不同之处吗? 四、AutoResetEvent和ManualResetEvent的初始状态 通过设置AutoResetEvent和ManualResetEvent构造函数可初始化收费站车闸状态: new Auto 如果将new AutoResetEvent(true) 改为new AutoResetEvent(flase),即车闸默认为关闭状态的话,将不会打印任何值,即车辆无法通过。 那如何才能通过呢? 五、用代码阐释AutoResetEvent的特性 代码: static EventWaitHandle _tollStation = new AutoResetEvent(false 这也表明,AutoResetEvent是典型的队列操作形式。
1.概要 什么是AutoResetEvent? AutoResetEvent是线程同步原语,它允许一个线程通知另一个线程应当继续执行。这可以用于控制两个或多个线程的交互和执行顺序。 如果AutoResetEvent处于未设定状态,则调用 WaitOne() 的线程会被阻塞,直到AutoResetEvent的状态通过调用 Set() 方法被设定。 优点 简单易用:使用AutoResetEvent可以方便地实现多个线程间的同步。 自动重置:AutoResetEvent在释放等待的线程后会自动切换到非信号状态。 ,直到AutoResetEvent再次被设定。 new AutoResetEvent(true); static AutoResetEvent autoEvent2 = new AutoResetEvent(false); static
看代码片段1: 代码片段1: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); private void BT_Temp_Click 而如果把: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); 改为: AutoResetEvent _autoResetEvent 也就是说,在终止状态中,_autoResetEvent.WaitOne()是不会起到阻滞工作线程的作用的。 (PS:ManualResetEvent也同样) 二:AutoResetEvent和ManualResetEvent的区别 接下来,再来看看AutoResetEvent和ManualResetEvent 我们看代码段2和代码段3: 代码段2: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); private void
AutoResetEvent可以在线程与线程间传递信号,来告知其他线程自己已经完成某一阶段的任务。 两个AutoResetEvent实例的初始状态都是unsignaled。 如果初始状态(new时给了true)为Signaled,则WaitOne会立刻执行,然后自动切换回unsignaled AutoResetEvent采用内核时间模式,等待时间不能太长。 工作完成 4等待中线程完成它的工作 5第一阶段工作完成 6在主线程上做了些操作 7现在开始由第二线程执行第二阶段 8开始第二阶段 9工作完成 10工作全部完成 private static AutoResetEvent _workerEvent = new AutoResetEvent(false); private static AutoResetEvent _mainEvent = new AutoResetEvent
看代码片段1: 代码片段1: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); private void _autoResetEvent.Set(); } void Thread1Foo() { _autoResetEvent.WaitOne 而如果把: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); 改为: AutoResetEvent _autoResetEvent (PS:ManualResetEvent也同样) 二:AutoResetEvent和ManualResetEvent的区别 接下来,再来看看AutoResetEvent和ManualResetEvent 我们看代码段2和代码段3: 代码段2: AutoResetEvent _autoResetEvent = new AutoResetEvent(false); private
Foo(AutoResetEvent autoResetEvent) { Task.Run(() => { = new AutoResetEvent(false); Foo(autoResetEvent); autoResetEvent.Set(); = new AutoResetEvent(false); Foo(autoResetEvent); autoResetEvent.Set(); = new AutoResetEvent(false); Foo(autoResetEvent); Foo2(autoResetEvent); autoResetEvent.Set(); for (int i = 0; i < 10; i++) { autoResetEvent.Set
阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同步方式可分为2种,用户模式构造和内核模式构造。 AutoResetEvent 使用示例如下,有简单注释。 关于描述,尽量贴近系统自身术语。 static void Main(string[] args) { //AutoResetEvent example //AutoResetEvent AutoResetEvent waitHandler = new AutoResetEvent(false);//false 即非终止,未触发。 这本应该是2步的操作,AutoResetEvent.set()函数,给2步一起自动做了,很方便。
——《微卡智享》 本文长度为3106字,预计阅读8分钟 前言 前一篇《学习|C#的EventHandler的委托使用》介绍了EventHandler的简单使用,本篇主要介绍线程中的AutoResetEvent AutoResetEvent简介 ? 微卡智享 AutoResetEvent对象用来进行线程同步操作,AutoResetEvent类继承waitHandle类。 AutoResetEvent的主要方法 # 主要方法 1 AutoResetEvent(bool initialState):构造函数,参数false:无信号,子线程的WaitOne方法不会被自动调用 上面就是AutoResetEvent的主要方法,从上面的主要方法中我们可以看到,实现读卡器每100耗秒进行检测,原来通过线程是sleep进行处理,现在可以使用WaitOne的方式,并且通过这个方法,我们可以在外部实现读卡器重连的调用 先定义一个AutoResetEvent,和等待的毫秒waitTime。 ? 然后定义一个Reset的方法,模拟读卡器重启。 ?
person.ShowHappy(); } } public class Person { //建立事件数组 public AutoResetEvent [] autoEvents = null; public Person() { autoEvents = new AutoResetEvent[ ] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; } public void GetCar() ();//AutoResetEvent继承WaitHandle 等同于:WaitHandle.WaitAll(); WaitHandles 的数目必须少于或等于 64 个。
这里我们自己实现一个异步版本的AutoResetEvent ---- 这里是我们创建的异步版本的AutoResetEvent。 首先AutoResetEvent一次只能有一个对象获得信号量。信号量被获取后马上会被reset。 那么我们就不能一直使用同一个TaskCompletionSource进行等待。 而AutoResetEvent在set时,获得同步锁的线程是随机的。 这里实际上无伤大雅,因为顺序也是随机的一种情况,在使用AutoResetEvent本身就不应该依赖顺序。 另外,我们在2019-12-1-使用SemaphoreSlim实现异步等待 - huangtengxiao中提到的SemaphoreSlim对象将最大并发数设置为1时,完全可以实现异步版本的AutoResetEvent 2019-12-1-使用SemaphoreSlim实现异步等待 - huangtengxiao 2019-12-1-实现一种异步版本的ManualResetEvent - huangtengxiao AutoResetEvent
AutoResetEvent在.Net多线程编程中经常用到。当某个线程调用WaitOne方法后,信号处于发送状态,该线程会得到信号, 程序就会继续向下执行,否则就等待。 而且 AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,其他调用WaitOne的线程只有继续等待.也就是说 ,AutoResetEvent一次只唤醒一个线程,其他线程还是堵塞。 简介 AutoResetEvent(bool initialState):构造函数,用一个指示是否将初始状态设置为终止的布尔值初始化该类的新实例。 如果是_autoResetEvent.set(),那么_autoResetEvent.WaitOne()后会自动将IsRelease的值自动设置为false。
var semaphoreSlim = new SemaphoreSlim(10, 10); private static readonly AutoResetEvent _autoResetEvent = new AutoResetEvent(true); 接下来进入循环创建线程,创建线程的时候先等待 AutoResetEvent 锁,而在线程执行的时候释放 AutoResetEvent 锁,这样就能让线程一定是在上一个线程执行之后再创建 而设置 AutoResetEvent 的初始值是通过,也就是第一个线程可以创建,但第二个线程需要等待第一个线程开始执行再创建 for (int i = 0; i < 1000; i++) { var n = i; _autoResetEvent.WaitOne(); _autoResetEvent = new AutoResetEvent(true); private static void GeregelkunoNeawhikarcee(SemaphoreSlim
2.2 AutoResetEvent AutoResetEvent是EventWaitHandle的一个简单包装,内部没有额外的任何逻辑。 例子,使用AutoResetEvent实现一个简单的线程同步锁。 但AutoResetEvent和ManualResetEvnet不行,他们没有提供类似的构造方法。 这个方法对于AutoResetEvent特别有用。但不太适合ManualResetEvent,因为要手动去调用Reset方法,不然会无限的调用这个方法。 private void TestAutoCallBack() { AutoResetEvent mre = new AutoResetEvent(false);
和 ManualResetEvent,关于这两者我们暂且认为是差不多了,稍后我会介绍他们的不同,这里以AutoResetEvent为例,其实很多官方的说法太过于抽象,这里通俗地讲,可以认为AutoResetEvent 个人认为它们最大的区别在于,无论何时,只要 AutoResetEvent 激活线程,它的状态将自动从终止变为非终止。 在上面代码中,如果将AutoResetEvent换成ManualResetEvent的话,只要要在waitone后面做下reset,就会达到同样的效果。 数组,主线程就WaitHandle.WaitAll(resets) ,子线程休眠完后就Set1个AutoResetEvent,最后都Set完后,主线程就会往下执行。 myResetEvent = new AutoResetEvent(false); static AutoResetEvent ChangeEvent = new AutoResetEvent
三、AutoResetEvent 有时候我们需要在线程之间通讯,我们可以借助数据库、文件进行解决,但是这都不是好办法。.NET 给我们提供了更好的办法–利用 AutoResetEvent 类。 我们利用 AutoResetEvent 类告诉等待执行的线程有事件要发生。 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号。 如果 AutoResetEvent 处于非终止状态,则该线程阻塞,并等待当前控制资源的线程通过调用 Set 发出资源可用的信号。调用 Set 向 AutoResetEvent 发信号以释放等待线程。 serveEvent = new AutoResetEvent(false); private static AutoResetEvent cookEvent = new AutoResetEvent 不同点: AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只有继续等待
= new AutoResetEvent(false); for (int i = 0; i < 100; i++) { var n = i; taskList.Add(Task.Run (() => { autoResetEvent.Set(); semaphore.Wait(); lock (locker) { Console.WriteLine(n); } semaphore.Release(); })); autoResetEvent.WaitOne = new AutoResetEvent(false); for (int i = 0; i < 100; i++) { var n = i; var thread = new Thread (() => { autoResetEvent.Set(); semaphore.Wait(); lock (locker)
AutoResetEvent、ManualResetEvent、Monitor、lock 等等这些用来做同步的类,如果在异步上下文(await)中使用,需要非常谨慎。 deadlock) - walterlv 不要使用 Dispatcher.Invoke,因为它可能在你的延迟初始化 Lazy<T> 中导致死锁 - walterlv 在有 UI 线程参与的同步锁(如 AutoResetEvent
构造函数 说明 AutoResetEvent(Boolean) 用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。 真糟糕的机器翻译。 .WaitOne() 用来等待另一个线程发送通知; .Set() 用来对线程发出通知,此时 AutoResetEvent 变成终止状态; .ReSet() 用来重置 AutoResetEvent 状态; 然后 AutoResetEvent 变为终止状态。 需要注意的是,如果 AutoResetEvent 已经处于终止状态,那么线程调用 WaitOne() 不会再起作用。除非调用Reset() 。 处于终止状态,无需等待信号 private static AutoResetEvent oneResetEvent = new AutoResetEvent(true); AutoResetEvent 也适合用于线程同步。