Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you stop the timer (by calling <code>Stop()</code> or by setting <code>Enabled = false</code>), then it will not fire again. I have <em>never</em> seen a case where a stopped timer calls the event handler. If the event handler is still being called after you disable the timer, then either some other code is calling it, or something is re-enabling the timer.</p> <p>Note also that if the event handler is currently executing, stopping the timer will not abort the event. It also won't prevent any pending events. Although that shouldn't be an issue with <code>System.Windows.Forms.Timer</code>, since its event handler always executes on the GUI thread.</p> <p>Were I you, I'd inspect the code very carefully and see all the places where the timer is enabled or disabled. I suspect you'll find that there is something in your code that's re-enabling the timer.</p> <p>Edit after seeing your posted code:</p> <p>Likely the problem is that in your timer event handler you have the following:</p> <pre><code>System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer(); // code that initializes and enables timer </code></pre> <p>That creates a local <code>timer2</code> variable, which will be different from your <code>timer2</code> that's at form scope (I assume, since you imply that the code compiles, and you reference a <code>timer2</code> in a separate scope). You end up creating a new timer, but the code that disables the timer is referencing the form-scope <code>timer2</code>. So what's happening is that you're creating a lot of different timers, and every one of them ends up calling the same event handler.</p> <p>I hope your calls to <code>MessageBox.Show</code> in the event handler are just for debugging purposes. You don't want to leave those there, because they block the UI thread and will prevent additional timer ticks until they're dismissed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload