title: System.Timers.Timer Elapsed event executing after timer.Stop() is called
body: <p><strong>Background:</strong> I have a timer that I am using to keep track of how long it has been since the serialPort DataReceived event has been fired. I am creating my own solution to this instead of using the built in timeout event because I am getting a continuous stream of data, instead of sending a query and getting one response. </p>
  
  <p><strong>The Problem:</strong>
  In the DataReceived handler I have a statement to stop the timer so that is doesn't elapse. the problem is that a lot of the time it still executes the Elapsed handler afterword.  </p>
  
  <p>I have read that is is possible to use SynchronizingObject to solve this problem but I am not sure how to accomplish that.</p>
  
  <p><strong>Here is my code:</strong> I tried to cut out everything that I didn't think was relevant.</p>
  
  <pre><code>    private System.Timers.Timer timeOut;
      private System.Timers.Timer updateTimer;
  
      public void start()
      {
          thread1 = new Thread(() =&gt; record());
  
          thread1.Start();
      }
  
      public void requestStop()
      {
          this.stop = true;
          this.WaitEventTest.Set();
  
      }
  
      private void record()
      {
          timeOut = new System.Timers.Timer(500); //** .5 Sec
          updateTimer = new System.Timers.Timer(500); //** .5 Sec
  
          timeOut.Elapsed += TimeOut_Elapsed;
          updateTimer.Elapsed += updateTimer_Elapsed;
          updateTimer.AutoReset = true;
  
  
          comport.Open();
          comport.DiscardInBuffer();
  
  
          comport.Write(COMMAND_CONTINUOUSMODE + "\r");
  
          stopwatch.Reset();
          stopwatch.Start();
  
          recordingStartTrigger(); //** Fire Recording Started Event
  
          timeOut.Start();
          updateTimer.Start();
  
          this.waitHandleTest.WaitOne(); //** wait for test to end
  
          timeOut.Stop();
          updateTimer.Stop();
  
          comport.Write(COMMAND_COMMANDMODE + Environment.NewLine);
          comport.DiscardInBuffer();
          comport.Close();
          recordingStopTrigger(status); //** Fire Recording Stopped Event
  
          stopwatch.Stop();
      }
  
  
      //***********************************************************************************
      //** Events Handlers
  
  
      private void comDataReceived_Handler(object sender, SerialDataReceivedEventArgs e)
      {
  
          double force = -100000;
          string temp = "-100000";
  
          //timeOut.SynchronizingObject.Invoke(new Action(()=&gt; {timeOut.Stop();}), new object[] {sender, e});
  
          timeOut.Stop();
  
          //** I removed my action code here, keep things simple.
  
  
          timeOut.Start();
      }
  
      private void TimeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          timeOut.Stop();
          updateTimer.Stop();
  
  
          //** fire delegate that GUI will be listening to, to update graph.
          if (eventComTimeOut != null &amp;&amp; this.stop == false)
          {
              if (eventComTimeOut(this, new eventArgsComTimeOut(comport.PortName, "READ")))
              {
                  //retry = true;
                  comport.Write(COMMAND_CONTINUOUSMODE + "\r");
                  updateTimer.Start();
                  timeOut.Start();
              }
              else
              {
                  this.stop = true;
                  //retry = false;
                  this.WaitEventTest.Set();
                  status = eventArgsStopped.Status.failed;                     
              }
          }
      }
  
      void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
  
          //** fire delegate that GUI will be listening to, to update graph.
          List&lt;Reading&gt; temp = new List&lt;Reading&gt;(report.Readings_Force);
          eventNewData(this, new eventArgsNewData(temp));
  
      }
  </code></pre>