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

查看:27
本文介绍了为什么 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 窗体上使用计时器,则将 System.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.

换句话说:

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

但同样,在这种情况下,使用 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天全站免登陆