关于ManualResetEvent使用c#的使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15656326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:30:25  来源:igfitidea点击:

Regarding the use of ManualResetEvent usage c#?

c#manualresetevent

提问by Thomas

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(); ?

can anyone give me small sample code where ManualResetEvent class usage will be there.

任何人都可以给我小示例代码,其中 ManualResetEvent 类的用法将在那里。

looking for good discuss and usage of ManualResetEvent ? thanks

正在寻找对 ManualResetEvent 的良好讨论和使用?谢谢

采纳答案by ken2k

I suggest you to read the "remarks" section of the MSDN page of ManualResetEventwhich is pretty clear about the usage of this class.

我建议您阅读MSDN 页面ManualResetEvent的“备注”部分其中对此类的用法非常清楚。

To answer your specific question, the ManualResetEventis used to simulate a synchronous call to Downloadeven if it's asynchronous. It calls the async method and blocks until the ManualResetEventis signaled. The ManualResetEventis signaled within the event handler of the async event-based pattern. So basically it waits until the event is fired and the event handler is executed.

为了回答您的具体问题,ManualResetEvent用于模拟同步调用,Download即使它是异步的。它调用异步方法并阻塞,直到ManualResetEvent发出信号。该ManualResetEvent是异步基于事件的模式的事件处理程序中发出信号。所以基本上它会等到事件被触发并执行事件处理程序。

回答by Радослав Тодоров

ManualSetEvent is class that helps you to manage communication between different threads when some thread must be stopped and wait to finish another thread (threads) then that class is very usefull.

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

回答by koviroli

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


VERY Simple explanation

http://dotnetpattern.com/threading-manualresetevent


非常简单的解释

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). This is almost the same as the above, but if for eaxample give 5 secondstime to this method, the current thread will be waiting for other thread to call the Set()method for 5 secondsand if no one called Set(), it calls it automatically and contunie the work.

WaitOne 的另一个重载是WaitOne(TimeSpan)。这和上面的几乎一样,但是如果例如给这个方法5 秒的时间,当前线程将等待其他线程调用Set()方法5 秒,如果没有人调用Set(),它自动调用它并继续工作。