Synchronizing a Timers.Timer elapsed method when stopping

后端 未结 5 1523
情书的邮戳
情书的邮戳 2020-12-19 09:18

With reference to this quote from MSDN about the System.Timers.Timer:

The Timer.Elapsed event is raised on a ThreadPool thread, so the event-handl

5条回答
  •  被撕碎了的回忆
    2020-12-19 09:26

    That is what it is suggesting.

    Monitor is the class that's used by the C# compiler for a lock statement.

    That being said, the above is only a problem if it is an issue in your situation. The entire statement basically translates to "You could get a timer event that happens right after you call Stop(). If this is a problem, you'll need to deal with it." Depending on what your timer is doing, it may be an issue, or it may not.

    If it's a problem, the Timer.Stop page shows a robust way (using Interlocked.CompareExchange) to handle this. Just copy the code from the sample and modify as necessary.

提交回复
热议问题