简体   繁体   English

关于使用ManualResetEvent的用法c#?

[英]Regarding the use of ManualResetEvent usage c#?

i am not familiar with the usage of ManualResetEvent ? 我不熟悉ManualResetEvent的用法?

is it thread related. 它与线程有关吗? what it does and when it is used? 它做什么以及何时使用?

here i got a code where ManualResetEvent is used but i just do not understand what it does? 在这里我得到了一个使用ManualResetEvent的代码,但我只是不明白它的作用?

here is the code 这是代码

public class Doc : SomeInterfaceFromTheDll
{
  private readonly IVersion version; // An interface from the DLL.
  private readonly ManualResetEvent _complete = new ManualResetEvent(false);

  private bool downloadSuccessful;

  // ...

  public bool Download()
  {
    this.version.DownloadFile(this);
    // Wait for the event to be signalled...
    _complete.WaitOne();
    return this.downloadSuccessful;
  }

  public void Completed(short reason)
  {
    Trace.WriteLine(string.Format("Notify.Completed({0})", reason));
    this.downloadSuccessful = reason == 0;
    // Signal that the download is complete
    _complete.Set();
  }

  // ...
} 

what is the meaning of _complete.WaitOne(); & _complete.Set(); ? _complete.WaitOne(); & _complete.Set(); ?的含义是什么_complete.WaitOne(); & _complete.Set(); ? _complete.WaitOne(); & _complete.Set(); ?

can anyone give me small sample code where ManualResetEvent class usage will be there. 任何人都可以给我一些小样本代码,其中包含ManualResetEvent类的用法。

looking for good discuss and usage of ManualResetEvent ? 寻找好的讨论和使用ManualResetEvent? thanks 谢谢

I suggest you to read the "remarks" section of the MSDN page of ManualResetEvent which is pretty clear about the usage of this class. 我建议你阅读ManualResetEventMSDN页面的“备注”部分,它非常清楚这个类的用法。

To answer your specific question, the ManualResetEvent is used to simulate a synchronous call to Download even if it's asynchronous. 为了回答您的具体问题, ManualResetEvent用于模拟对Download的同步调用,即使它是异步的。 It calls the async method and blocks until the ManualResetEvent is signaled. 它调用异步方法并阻塞,直到发出ManualResetEvent信号。 The ManualResetEvent is signaled within the event handler of the async event-based pattern. ManualResetEvent在基于异步事件的模式的事件处理程序中发出信号。 So basically it waits until the event is fired and the event handler is executed. 所以基本上它会等到事件被触发并执行事件处理程序。

To achieve deep understanding of any subject, I have to read the almost same information in other words. 为了深入理解任何主题,我必须阅读几乎相同的信息。 I've read the MSDN documentation about the ManualResetEvent, it was good I almost got to understand it, but this link helped me to understand it well: 我已经阅读了有关ManualResetEvent的MSDN文档,我很了解它很好,但这个链接帮助我理解它:

http://dotnetpattern.com/threading-manualresetevent http://dotnetpattern.com/threading-manualresetevent


VERY Simple explanation 非常简单的解释

If the current thread calls the WiatOne() method, it will be waiting(so stop doing anything) until any other thread calls the Set() method. 如果当前线程调用WiatOne()方法,它将等待(所以停止做任何事情),直到任何其他线程调用Set()方法。

There is another overload for the WaitOne, is the WaitOne(TimeSpan) . WaitOne还有另一个重载,就是WaitOne(TimeSpan) This is almost the same as the above, but if for eaxample give 5 seconds time to this method, the current thread will be waiting for other thread to call the Set() method for 5 seconds and if no one called Set() , it calls it automatically and contunie the work. 这与上面的几乎相同,但是如果对于eaxample给这个方法5秒的时间,当前线程将等待其他线程调用Set()方法5秒 ,如果没有人调用Set() ,它自动调用它并使工作恢复正常。

ManualSetEvent是一个类,它可以帮助您管理不同线程之间的通信,当一些线程必须停止并等待完成另一个线程(线程)然后该类非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM