简体   繁体   English

C# - Winform 定时器 - 处理和清空定时器

[英]C# - Winform Timer - Disposing and emptying the timer

Fairly new to C# and timers, although I've managed to do some really fun stuff in C#, however I'm not getting the hang of Timers. C# 和计时器相当新,虽然我已经设法在 C# 中做了一些非常有趣的事情,但是我没有掌握计时器的窍门。

Form1.cs: Form1.cs:

private int counter;
static System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public void goTimer()
{
    // Set Counter
    counter = 60;

    // If timer is already enabled, stop it.
    if (timer1.Enabled)
    {
        timer1.Dispose();
        //timer1.Stop() <- also tried

    }

    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 1000; // 1 second
    timer1.Start(); // Timer exists

    txtCountdown.Text = counter.ToString();

}

private void timer1_Tick(object sender, EventArgs e)
{
    counter--;
    if(counter == 0)
    {
        timer1.Stop();
    }
    txtCountdown.Text = counter.ToString();
}

So, what happens is that it seems to work as intended, until you start calling goTimer();所以,发生的事情是它似乎按预期工作,直到你开始调用goTimer(); from eg a button press, then it will speed up the (int) counter as many times as you pressed it... And after a while the memory will be eaten up.从例如按下按钮,然后它会加快(int) counter的速度,就像你按下它的次数一样......一段时间后,memory 将被吃掉。

In this case the users will be able to do call the timer function, as it will remove some objects, clear some data and refresh the session, but also when the timer reaches 0.在这种情况下,用户将能够调用定时器 function,因为它会删除一些对象,清除一些数据并刷新 session,而且当定时器达到 0 时。

Using Winforms, I did not add a timer in visual studio (it's only referenced here in Form1.cs).使用 Winforms,我没有在 Visual Studio 中添加计时器(仅在 Form1.cs 中引用)。

How do I terminate all timers, and then restart at (int) counter ?如何终止所有计时器,然后在 (int) counter处重新启动?

Using start and stop of the timer would be the proper aproach, but generally also the dispose variant will work.使用timer的启动和停止将是正确的方法,但通常也可以使用 dispose 变体。

Your memory hole results from the multiplied event handler assignments, you need to move this method to your constructor or some other initialization method:您的 memory 孔是由多重事件处理程序分配产生的,您需要将此方法移动到您的构造函数或其他一些初始化方法:

timer1.Tick += new EventHandler(timer1_Tick);

If you really want to create a new timer every time, you need to release the event handler before:如果你真的想每次都创建一个新的定时器,你需要在之前释放事件处理程序:

timer1.Tick -= timer1_Tick;

First of all, as MichaelSander already mentioned, you should put these lines in your Form1.cs constructor:首先,正如 MichaelSander 已经提到的,您应该将这些行放在您的Form1.cs构造函数中:

timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; // 1 second

Secondly, there is no point in disposing your timer if it's meant to be used more than once.其次,如果要多次使用计时器,则处置计时器是没有意义的。 Instead of timer1.Dispose() you should use timer1.Stop() just like you do in your timer1_Tick handler.您应该使用timer1.Stop()而不是timer1.Dispose() (),就像在timer1_Tick处理程序中一样。 Also there is no point in checking whether the timer is enabled or disabled as both timer1.Start() and timer1.Stop() will either turn it on/off respectively or do nothing at all.此外,检查计时器是启用还是禁用也没有意义,因为timer1.Start()timer1.Stop()将分别打开/关闭它或什么都不做。 That means that in your case you can remove this block completely:这意味着在您的情况下,您可以完全删除此块:

if (timer1.Enabled) { timer1.Dispose(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM