The timer just won’t die (already!)

For some time I had this recurring nightmare (read error in my application). For some reason, a certain timer continues to send Expired events after I stopped it, although in the event itself the timer “allows” to turn off! Check this:

//Timer is created in Class' Constructor. Class is not static.
public PDAAccess ()
{
  ConnectionTimeoutChecker = new System.Timers.Timer(1000);
  ConnectionTimeoutChecker.Elapsed += new System.Timers.ElapsedEventHandler(ConnectionTimeoutChecker_Elapsed);
}

void ConnectionTimeoutChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ //_DeviceConTimeout eventually reaches A LOT MORE than 10.
  if (_DeviceConTimeout > 10)
  {
    ConnectionTimeoutChecker.Stop(); //This is invoked but the timer DOES NOT STOP.
    if (OnDeviceSyncTimeout != null) OnDeviceSyncTimeout(this, null); //This gets fired all the time.
  }
  _DeviceConTimeout++; //This keeps increasing and increasing.
  //Worth mentioning: sender = Timer, sender.Enabled = false (!) so then why is this executing?
}

As for where I run it: I run it in one place, I put a breakpoint there, and it fails more than once. And before you ask: multiple threads are not involved. I work with streams in this application, but: the timer is not created in the stream, and is not a class.

But .Stop (); runs 100 times and the timer still does NOT stop.

. , , , , -. , "" ( " " ). , ... , :: D.

+3
2

, , . , , .

System.Timers.Timer - . , , "", , . , Elapsed, ThreadPool.QueueUserWorkItem(). Windows. QUWI , . " ". TP , . , , , . , . , .

Windows . , , , , . , , . -, , - Elapsed, .

, . , Elapsed. TP , QUWI, TP, , .

, , . System.Thread.Timer . . - - , .

0

: , OnDeviceSyncTimeout() - ? , , , Im , .

+1

Source: https://habr.com/ru/post/1758604/


All Articles