What is the difference between ManualResetEvent and AutoResetEvent in .NET?

后端 未结 11 2197
慢半拍i
慢半拍i 2020-11-27 09:06

I have read the documentation on this and I think I understand. An AutoResetEvent resets when the code passes through event.WaitOne(), but a ManualResetEvent do

相关标签:
11条回答
  • 2020-11-27 09:20

    Yes, thats right.

    You can get an idea by the usage of these two.

    If you need to tell that you are finished with some work and other (threads) waiting for this can now proceed, you should use ManualResetEvent.

    If you need to have mutual exclusive access to any resource, you should use AutoResetEvent.

    0 讨论(0)
  • 2020-11-27 09:25

    Yes. This is absolutely correct.

    You could see ManualResetEvent as a way to indicate state. Something is on (Set) or off (Reset). An occurrence with some duration. Any thread waiting for that state to happen can proceed.

    An AutoResetEvent is more comparable to a signal. A one shot indication that something has happened. An occurrence without any duration. Typically but not necessarily the "something" that has happened is small and needs to be handled by a single thread - hence the automatic reset after a single thread have consumed the event.

    0 讨论(0)
  • 2020-11-27 09:27

    If you want to understand AutoResetEvent and ManualResetEvent you need to understand not threading but interrupts!

    .NET wants to conjure up low-level programming the most distant possible.

    An interrupts is something used in low-level programming which equals to a signal that from low became high (or viceversa). When this happens the program interrupt its normal execution and move the execution pointer to the function that handles this event.

    The first thing to do when an interrupt happend is to reset its state, becosa the hardware works in this way:

    1. a pin is connected to a signal and the hardware listen for it to change (the signal could have only two states).
    2. if the signal changes means that something happened and the hardware put a memory variable to the state happened (and it remain like this even if the signal change again).
    3. the program notice that variable change states and move the execution to a handling function.
    4. here the first thing to do, to be able to listen again this interrupt, is to reset this memory variable to the state not-happened.

    This is the difference between ManualResetEvent and AutoResetEvent.
    If a ManualResetEvent happen and I do not reset it, the next time it happens I will not be able to listen it.

    0 讨论(0)
  • 2020-11-27 09:28

    Just imagine that the AutoResetEvent executes WaitOne() and Reset() as a single atomic operation.

    0 讨论(0)
  • 2020-11-27 09:28

    Taken from C# 3.0 Nutshell book, by Joseph Albahari

    Threading in C# - Free E-Book

    A ManualResetEvent is a variation on AutoResetEvent. It differs in that it doesn't automatically reset after a thread is let through on a WaitOne call, and so functions like a gate: calling Set opens the gate, allowing any number of threads that WaitOne at the gate through; calling Reset closes the gate, causing, potentially, a queue of waiters to accumulate until its next opened.

    One could simulate this functionality with a boolean "gateOpen" field (declared with the volatile keyword) in combination with "spin-sleeping" – repeatedly checking the flag, and then sleeping for a short period of time.

    ManualResetEvents are sometimes used to signal that a particular operation is complete, or that a thread's completed initialization and is ready to perform work.

    0 讨论(0)
  • 2020-11-27 09:29

    autoResetEvent.WaitOne()

    is similar to

    try
    {
       manualResetEvent.WaitOne();
    }
    finally
    {
       manualResetEvent.Reset();
    }
    

    as an atomic operation

    0 讨论(0)
提交回复
热议问题