线程中的ManualResetEvent和AutoResetEvent [英] ManualResetEvent and AutoResetEvent in Thread

查看:77
本文介绍了线程中的ManualResetEvent和AutoResetEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ManualResetEvent和AutoResetEvent是什么意思?请举个例子吗?

What is mean by ManualResetEvent and AutoResetEvent? please give an Example ?

推荐答案

这两个类型的继承是从System.Threading.EventWaitHandle派生的,并且没有添加任何功能.使用它们等效于将EventWaitHandle与不同的构造函数参数一起使用.第一个参数定义重置是否自动完成.因此,让我们仅考虑此类,请参见 http://msdn.microsoft.com /en-us/library/system.threading.eventwaithandle.aspx [ ^ ].

让我们看一下页面结尾处的构造函数和代码示例: http://msdn .microsoft.com/en-us/library/csc3y90t.aspx [ ^ ].

请在代码示例中找到此字符串:"ewh.WaitOne();".

这是一个阻止呼叫.事件等待句柄有两种状态:有信号的,可以通过调用ewh.Set()将其置于此状态,以及无信号的,可以通过调用ewh.Reset()将其置于此状态.上面显示的阻塞调用将调用线程置于OS的等待状态.线程被关闭,使用零CPU时间,并且直到被唤醒才调度到执行.可以通过超时(如果有)或其他可以调用Thread.Abort或发信号通知ewh.Set()的线程来唤醒它.

当事件等待句柄唤醒线程时,ehw状态会发生什么?它取决于传递给该对象的构造函数的EventResetMode(请参阅上面引用的文章中的构造函数签名).如果此模式为EventResetMode.ManualReset,则其状态将保持信号状态,这会影响此线程或另一个调用ewh.WaitOne();的线程的行为.

如果此模式为EventResetMode.AutoReset,则在ewh.WaitOne();中等待的线程为 atomic 方式时,它将自动进入非信号状态. 原子"在这里非常重要:您无法使用手动重置以100%可靠的方式模拟此行为.自动重置旨在仅传递一个线程并让所有其他线程等待,这是某些技术中非常有价值的行为.

—SA
These two type inherit are derived from System.Threading.EventWaitHandle and add no functionality to it. Using them is equivalent to using EventWaitHandle with different constructor parameters; first parameter defines if the reset done automatically or not. So let''s consider just this class, see http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^].

Let''s look at the constructor and the code sample at the end of this page: http://msdn.microsoft.com/en-us/library/csc3y90t.aspx[^].

Please locate this string in the code sample: "ewh.WaitOne();".

This is a blocking call. The event wait handle has two states: signaled, one can put it to this state by calling ewh.Set() and non-signaled, one can put it to this state by calling ewh.Reset(). The blocking call shown above puts a calling thread in a wait state by OS; the thread is switched off, use zero CPU time and is never scheduled back to execution until it is waken up. It can be waken by by timeout (if any) and also by some other thread which can call Thread.Abort or signal the ewh.Set().

When a thread is waken up by the event wait handle, what happens to the ehw state? It depends on the EventResetMode passed to this object''s constructor (please see the constructor signature in the article referenced above). If this mode is EventResetMode.ManualReset, its state remains signaled, which effects the behavior of this or another thread calling ewh.WaitOne();.

If this mode is EventResetMode.AutoReset, it is automatically gets the non-signaled state when the thread waiting in ewh.WaitOne(); is atomic way. "Atomic" if very important here: you cannot possibly simulate this behavior on 100% reliable way using manual reset. Auto-reset is designed to pass one and only one thread and keep all other threads waiting, which is very valuable behavior is some techniques.

—SA


这篇关于线程中的ManualResetEvent和AutoResetEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆