title: powershell timer cannot stop immediately
body: <p>I am trying using powershell Timer to do some periodically job and once the job returns results that meet my expectation, I will stop running the job.</p>
  
  <pre><code>&gt;$timer = New-Object System.Timers.Timer
  &gt;$counter = 0
  &gt;$timer.Interval = 1000
  &gt;$timer.AutoReset = $true
  &gt;Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action { if ( $counter -eq 5 ) {$timer.Stop(); } ; $counter = $counter +1; write-host "counter is: " $counter; }
  &gt;$timer.Start()
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code> counter is:  1
   counter is:  2
   counter is:  3
   counter is:  4
   counter is:  5
   counter is:  6
  </code></pre>
  
  <p>I was expecting the timer to stop once $counter reaches 5, but it continues to work. Is there any way to <strong>immediately</strong> stop the timer once some condition is satisfied?</p>