更新时间:2023-12-06 11:07:22
如果你不保持一个参考定时器对象,这将是垃圾收集。
If you don't keep a reference to the timer object, it will be garbage collected.
看着你已经张贴了code,它不会出现要保持保持基准的。你需要使它成为一个领域的包含类,而不是一个局部变量。
Looking at the code that you've posted, it doesn't appear that you are keeping hold of the reference. You will need to make it a field in your containing class, rather than a local variable.
定时器还可以得到垃圾回收,如果你在一个长期运行的方法开始申报,并没有后来引用它的方法。
The timer can also get garbage collected if you declare it at the start of a long-running method and don't reference it later in the method.
您可以通过添加 GC.KeepAlive(定时器)解决特定的问题;
附近的方法的这里描述。
You can fix that particular problem by adding GC.KeepAlive(timer);
near the end of the method as described here.