C# - how do you stop a timer?

Solution 1:

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(); 

Solution 2:

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)

Solution 3:

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();