在C#中,我应该让我的计时器的参考? [英] In C#, where should I keep my timer's reference?

查看:142
本文介绍了在C#中,我应该让我的计时器的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Threading.Timer 的文档中说,我要保持住参考,以避免它被垃圾收集。但是,在我该怎么办呢?我的很简单,我不知道在哪里,以保持引用:

The documentation of System.Threading.Timer says that I should keep a live reference for it to avoid it being garbage collected. But where should I do that? My main is very simple that I don't know where to keep the reference:

class Program {
    static void Main() {
        new System.Threading.Thread(myThreadStart).Start();
        new System.Threading.Timer(myTimerCallback, new MyStateObject(), 0, 5000);
    }
}

我想过保持在静态字段中引用的计划类,假设静态字段没有得到收集,直到应用程序的结束。但我不知道这是做的最好的办法,所以我AP preciate您的咨询。

I thought about keeping the reference in a static field in the Program class, assuming that static fields do not get collected until the end of the application. But I'm not sure this is the best way to do it, so I'd appreciate your advice.

推荐答案

如果您的计时器是一个应用程序级的对象有什么不对使得你的主类的私有静态成员。这就是我会做,反正。

If your Timer is an application-level object there's nothing wrong with making it a private static member of your Main class. That's what I would do, anyway.

这篇关于在C#中,我应该让我的计时器的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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