Why does System.Timer.Timer still fire events when Enabled is set to false?

前端 未结 5 550
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 04:35

I have attempted to create a derived class of Timer that allows for a \'Pause\' latch to be set to keep the worker thread from reactivating the timer. However, Elapsed event

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 04:59

    Everything is more complex in multithreading, I'm afraid. Assuming your code is working as you wish, there is a window where in-flight events can get raised after you reset the Enabled property. See this quote from the MSDN docs.

    The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. This might result in the Elapsed event being raised after the Enabled property is set to false. The code example for the Stop method shows one way to work around this race condition.

提交回复
热议问题