title: What is the proper way to dispose of System.Timers.Timer?
body: <p>I have a problem with the Timer class in that there is a chance that a timer would fire right before you call Dispose() since you will probably call Dispose() from some other thread. You may have something like:</p>
  
  
  
  <pre class="lang-vb prettyprint-override"><code>Public Sub TimerTick() Handles itsTimer.Elapsed
     Do Something
  End Sub
  </code></pre>
  
  <p>And then from somewhere else in the code, when your app is done, you call</p>
  
  <pre class="lang-vb prettyprint-override"><code>itsTimer.Dispose()
  </code></pre>
  
  <p>The only way I can think of doing this is:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Sub SomeMethod()
     SyncLock(itsTimer.SynchronizingObject)
          itsTimer.Dispose()
     End SyncLock
  ENd Sub
  </code></pre>
  
  <p>This is not a great thing to do esp. since I am holding a lock on an object I am disposing. I can create my own lock and syncLock on that but is there a simple way?</p>