In C#, where should I keep my timer's referenc

2019-02-06 04:38发布

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

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.

3条回答
该账号已被封号
2楼-- · 2019-02-06 05:05

EDIT: My original answer is rubbish. Really rubbish. I've kept it here to explain why it's rubbish though - it's in the comments, but they'd have been deleted with the answer.

GC.KeepAlive only makes sure a reference is treated as a root until after the call. In the code at the bottom of this answer, the GC.KeepAlive method would be called immediately, and then the timer would still be eligible for garbage collection. Because the newly created thread is a foreground thread, the app will run as long as it's alive (whereas the timer uses a background thread, which doesn't prevent program exit). This means that the Main method exits, but the application needs to keep running.

Arguably a simpler solution would be to run myThreadStart in the main thread, rather than creating a new one and then letting the main thread die. In other words, a simple solution would be:

using System.Threading;

class Program {
    static void Main() {
        Timer timer = new Timer(myTimerCallback, 
                                new MyStateObject(), 0, 5000);
        myThreadStart();
        GC.KeepAlive(timer);
    }
}

I assume the real code is more complicated though - in which case using a private static variable as suggested in other answers is probably the way to go. It really will depend on the usage though. I personally prefer not to create a static field just for the sake of preventing something being collected if there's an alternative (like the above) but sometimes it's virtually the only way of doing it.

Original (bad) answer:

If you really want to allocate it in Main, then you can use GC.KeepAlive:

using System.Threading;

class Program {
    static void Main() {
        new Thread(myThreadStart).Start();
        Timer timer = new Timer(myTimerCallback, 
                                new MyStateObject(), 0, 5000);
        GC.KeepAlive(timer);
    }
}
查看更多
Viruses.
3楼-- · 2019-02-06 05:11

I think it is OK to keep is a private static field of your class.

I would keep this reference as a static field rather than playing with garbage collector.

查看更多
放我归山
4楼-- · 2019-02-06 05:13

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.

查看更多
登录 后发表回答