C# - 你怎么停止计时器?

时间:2021-10-06 20:35:43

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.

我知道这听起来很愚蠢,但我已经尝试了一切来停止计时器,但计时器不会停止。我正在做一个游戏,如果有人能告诉我如何停止计时器,我将不胜感激。

6 个解决方案

#1


112  

If you are using System.Timers.Timer you can stop like this

如果您使用的是System.Timers.Timer,则可以这样停止

timer.Enabled = false

if you are using System.Threading.Timer, use this

如果您使用的是System.Threading.Timer,请使用此选项

timer.Change(Timeout.Infinite , Timeout.Infinite)

Or use

或者使用

timer.Stop(); 

if you are using System.Windows.Forms.Timer

如果您使用的是System.Windows.Forms.Timer

#2


19  

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

System.Windows.Forms.Timer:timer.Enabled = false; System.Threading.Timer:timer.Change(Timeout.Infinite,Timeout.Infinite); System.Timers.Timer:timer.Enabled = false;或timer.Stop();

#3


19  

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:

因此,要添加到以前的答案,如果您使用的是System.Threading.Timer类,这将永久停止它,不再有机会使用相同的实例:

   timer.Dispose()

otherwise:

除此以外:

  timer.Change(Timeout.Infinite, Timeout.Infinite)

#4


5  

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.

对于.NET框架中的每个计时器,计时器可能会在您停止之前触发,因此您在停止后会看到回调。

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.

您需要使用类似异步回调上下文的方法:当您希望计时器运行时,将bool设置为true,并在停止时将其设置为false。然后让你的回调检查你的上下文,看看它是否真的应该运行。

#5


1  

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;因为没有明确提及其他任何事情......如果是这样的话......

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Stop(); 

#6


1  

Depends on the timer. If it is from threading namespace, dispose of it and recreate it when you need to, or have your timer delegate wait on reset event(see msdn). System.Timers namespace has a start and stop method.

取决于计时器。如果它来自线程命名空间,请将其丢弃并在需要时重新创建它,或让你的计时器委托等待重置事件(参见msdn)。 System.Timers命名空间有一个start和stop方法。

#1


112  

If you are using System.Timers.Timer you can stop like this

如果您使用的是System.Timers.Timer,则可以这样停止

timer.Enabled = false

if you are using System.Threading.Timer, use this

如果您使用的是System.Threading.Timer,请使用此选项

timer.Change(Timeout.Infinite , Timeout.Infinite)

Or use

或者使用

timer.Stop(); 

if you are using System.Windows.Forms.Timer

如果您使用的是System.Windows.Forms.Timer

#2


19  

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

System.Windows.Forms.Timer:timer.Enabled = false; System.Threading.Timer:timer.Change(Timeout.Infinite,Timeout.Infinite); System.Timers.Timer:timer.Enabled = false;或timer.Stop();

#3


19  

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:

因此,要添加到以前的答案,如果您使用的是System.Threading.Timer类,这将永久停止它,不再有机会使用相同的实例:

   timer.Dispose()

otherwise:

除此以外:

  timer.Change(Timeout.Infinite, Timeout.Infinite)

#4


5  

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.

对于.NET框架中的每个计时器,计时器可能会在您停止之前触发,因此您在停止后会看到回调。

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.

您需要使用类似异步回调上下文的方法:当您希望计时器运行时,将bool设置为true,并在停止时将其设置为false。然后让你的回调检查你的上下文,看看它是否真的应该运行。

#5


1  

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;因为没有明确提及其他任何事情......如果是这样的话......

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Stop(); 

#6


1  

Depends on the timer. If it is from threading namespace, dispose of it and recreate it when you need to, or have your timer delegate wait on reset event(see msdn). System.Timers namespace has a start and stop method.

取决于计时器。如果它来自线程命名空间,请将其丢弃并在需要时重新创建它,或让你的计时器委托等待重置事件(参见msdn)。 System.Timers命名空间有一个start和stop方法。