I know it sounds stupid, but I've tried everything to stop a timer, but the timer won't stop. I'm working on a game and i would appreciate if someone could tell me how to stop a timer.
-
11post your code for a good start. – Pabuc Dec 30 '10 at 15:12
-
3Which of the timer classes? There are 4 that I know of, just in the BCL. – Oded Dec 30 '10 at 15:12
-
On top, at least try to tell us what you tried to stop it. "Read the documentation" is otherwise the only valid answer. – TomTom Dec 30 '10 at 15:16
-
press the button on top! :P - show some code please – Rippo Dec 30 '10 at 15:19
7 Answers
If you are using System.Timers.Timer
stopping is performed by one of the options:
//options 1
timer.Enabled = false
//option 2
timer.Stop()
if you are using System.Threading.Timer
, use this method
timer.Change(Timeout.Infinite , Timeout.Infinite)
if you are using System.Windows.Forms.Timer
, use this method
timer.Stop();

- 2,981
- 2
- 31
- 47

- 49,896
- 32
- 148
- 184
-
1it is also interesting to see that System.Timers.Timer is using a System.Threading.Timer internally, and the Enable=false disposes the internal timer, and re creates it on Enable=true / Start(): https://source.dot.net/#System.ComponentModel.TypeConverter/System/Timers/Timer.cs,897683f27faba082 – EricBDev Jul 15 '21 at 08:52
-
Sorry but where do you get timer to stop it? I do `timer.Elapsed += new ElapsedEventHandler(this.OnTimer1); protected void OnTimer1(object sender, ElapsedEventArgs args) { timer.STop(); }` and compiler raise error `the name 'timer' does not exists in this current context`? – Pham X. Bach May 27 '22 at 02:25
-
@PhamX.Bach if you are using designer to put the timer, check the name of that timer in the properties ... otherwise you should have been created timer somewhere in your code to stop it later – Arsen Mkrtchyan May 27 '22 at 17:09
So to add to the previous answers, in case you are using the System.Threading.Timer class, this will stop it permanently with no further chance to use the same instance:
timer.Dispose()
otherwise:
timer.Change(Timeout.Infinite, Timeout.Infinite)

- 7,063
- 9
- 54
- 71
System.Windows.Forms.Timer: timer.Enabled = false;
System.Threading.Timer: timer.Change(Timeout.Infinite, Timeout.Infinite);
System.Timers.Timer: timer.Enabled = false;
or timer.Stop();

- 25,272
- 21
- 67
- 103
With each of the timers in the .NET framework, it's possible that the timer fires just before you stop it, so you'll see the callback after you stop it.
You'll need to use something like an asynchronous callback context: use a bool
set to true
when you want the timer running, and set it to false
when you stop it. Then have your callback check your context to see if it should really run or not.

- 437,863
- 77
- 675
- 810
-
3I am glad I have found this information here too. I am using, instead of extra variable, checking if timer is still enabled in timer callback. – Fanda Mar 06 '15 at 06:48
Assuming you are making use of the System.Windows.Forms.Timer; since there was no explicit reference to anything else...if that is the case...
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Stop();

- 24,527
- 5
- 59
- 88
I also ran into the similar problem many times.
//Timer init.
var _timer = new System.Timers.Timer
{
AutoReset = true,
Enabled = true,
Interval = TimeSpan.FromSeconds(15).TotalMilliseconds //15 seconds interval
};
_timer.Elapsed += DoSomethingOnTimerElapsed;
//To be called on timer elapsed.
private void DoSomethingOnTimerElapsed(object source, ElapsedEventArgs e)
{
//Disable timer.
_timer.Enabled = false; //or _timer.Stop()
try
{
//does long running process
}
catch (Exception ex)
{
}
finally
{
if (_shouldEnableTimer) //set its default value to true.
_timer.Enabled = true; //or _timer.Start()
}
}
//somewhere in the code if you want to stop timer:
_timer.Enabled = _shouldEnableTimer = false;
//At any point, if you want to resume timer add this:
_timer.Enabled = _shouldEnableTimer = true;
Why to do so?
Lets assume, the code inside the try block takes more time. So, by the time you disable timer (_timer.Enabled = false or _timer.Stop()
), there is high possibilty that the code inside try block is still executing. Hence, after completion of the task when it comes to finally, it is again enabled if there is no flag(_shouldEnableTimer
) check. Therefore, I prevent your problem by adding an additional flag check.
For more clarity, please go through the code and the added comments. Hope this helps.

- 1,858
- 4
- 27
- 44