Synchronizing a Timers.Timer elapsed method when stopping

后端 未结 5 1494
情书的邮戳
情书的邮戳 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.

    0 讨论(0)
  • 2020-12-19 09:28

    Stopping a System.Timers.Timer reliably is indeed a major effort. The most serious problem is that the threadpool threads that it uses to call the Elapsed event can back up due to the threadpool scheduler algorithm. Having a couple of backed-up calls isn't unusual, having hundreds is technically possible.

    You'll need two synchronizations, one to ensure you stop the timer only when no Elapsed event handler is running, another to ensure that these backed-up TP threads don't do any harm. Like this:

        System.Timers.Timer timer = new System.Timers.Timer();
        object locker = new object();
        ManualResetEvent timerDead = new ManualResetEvent(false);
    
        private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
            lock (locker) {
                if (timerDead.WaitOne(0)) return;
                // etc...
            }
        }
    
        private void StopTimer() {
            lock (locker) {
                timerDead.Set();
                timer.Stop();
            }
        }
    

    Consider setting the AutoReset property to false. That's brittle another way, the Elapsed event gets called from an internal .NET method that catches Exception. Very nasty, your timer code stops running without any diagnostic at all. I don't know the history, but there must have been another team at MSFT that huffed and puffed at this mess and wrote System.Threading.Timer. Highly recommended.

    0 讨论(0)
  • 2020-12-19 09:38

    Try:

    lock(timer) {
    timer.Stop();
    }
    
    0 讨论(0)
  • 2020-12-19 09:40

    Seems timer is not thread safe. You must keep all calls to it in sync via locking. lock(object){} is actually just short hand for a simple monitor call.

    0 讨论(0)
  • 2020-12-19 09:47

    Here is a very simple way to prevent this race condition from occurring:

    private object _lock = new object();
    private Timer _timer; // init somewhere else
    
    public void StopTheTimer()
    {
        lock (_lock) 
        {
            _timer.Stop();
        }
    }
    
    void elapsed(...)
    {
        lock (_lock)
        {
            if (_timer.Enabled) // prevent event after Stop() is called
            {
                // do whatever you do in the timer event
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题