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

后端 未结 11 2205
慢半拍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: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.

提交回复
热议问题