为什么System.Threading.Timer自行停止? [英] Why does System.Threading.Timer stop on its own?

查看:455
本文介绍了为什么System.Threading.Timer自行停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个小测试项目之前,我在一个Windows服务项目中使用 System.Threading.Timer 。它的工作奇妙,但计时器一两分钟后自行停止

I'm doing a small test project before I use System.Threading.Timer in a Windows Service project. It's working wonderfully, however the timer stops on its own after a minute or two.

的全部源代码的测试项目为:

The full source for the test project is:

using System;
using System.Windows.Forms;
using System.Threading;

namespace studyTimers {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            TimerCallback timerDelegate = new TimerCallback(tick);
            System.Threading.Timer testTimer = new System.Threading.Timer(timerDelegate, null, 1000, 1000);
        }

        void tick(Object obj) {
            if (label1.InvokeRequired) {
                label1.Invoke(new MethodInvoker(() => tick(obj)));
            } else {
                label1.Text = DateTime.Now.ToString();
            }
        }
    }
}



目标显然是要更新与当前时间的标签。我注意到,更新了一下后停止。为什么会这样呢?

The goal is obviously to update a label with the current time. I am noticing that updating stops after a bit. Why would this be?

推荐答案

如果你需要一个Windows窗体上的计时器跌得系统。 Windows.Forms.Timer 到窗体 - 没有理由使用 System.Threading.Timer 除非你需要比55毫秒更好的分辨率

If you need a timer on a Windows Form then drop a System.Windows.Forms.Timer onto the form - there's no reason to use a System.Threading.Timer unless you need better resolution than 55 ms.

计时器停止的原因是因为它被垃圾收集。你允许它走出去的范围在 Form1_Load的方法,因为你只把它声明为一个局部变量。为了保持计时器活着,它需要在表单类,以便GC知道它仍然需要

The reason the timer "stops" is because it's being garbage-collected. You're allowing it to go out of scope in the Form1_Load method because you only declare it as a local variable. In order to keep the timer "alive", it needs to be a private field on the form class so that the GC knows it's still needed.

在换句话说一个私有字段:

In other words:

public partial class Form1 : Form
{
    private System.Threading.Timer testTimer;

    ...

    public void Form1_Load(object sender, EventArgs e)
    {
        TimerCallback timerDelegate = new TimerCallback(tick);
        testTimer = new System.Threading.Timer(timerDelegate, null, 1000, 1000);
    }
}



但同样,在这种情况下,它simplier使用 System.Windows.Forms.Timer ,这是在说你可以拖放到窗体工具箱中的实际组成部分。

But again, in this case it's simplier to use System.Windows.Forms.Timer, which is an actual component in the toolbox that you can just drop onto the form.

修改的 - 正如现在的注释透露,如果这仅仅是一个测试应用程序和实际应用是一个Windows服务,您的不可以的使用 System.Windows.Forms.Timer 为。只要记住,不要让你的 System.Threading.Timer 走出去的范围。

Edit - As the comments now reveal, if this is just a test app and the real application is in a Windows Service, you cannot use System.Windows.Forms.Timer for that. Just remember not to let your System.Threading.Timer go out of scope.

这篇关于为什么System.Threading.Timer自行停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆