这个Timer会从内存中释放吗?

发布于 2024-10-19 01:05:57 字数 738 浏览 5 评论 0原文

考虑一下 C# 中的这对函数:

void func1() {
    DispatcherTimer tmr = new DispatcherTimer();
    tmr.Interval = TimeSpan.FromSeconds(5);
    tmr.Tick += func2;
    tmr.Start();
}

void func2(object a, EventArgs b) {
    // Called every 5 seconds once func1() is called
}

调用 func1() 一次后,从那时起,func2() 每 5 秒调用一次,尽管我失去了对计时器的引用,因为它的范围仅限于 func1()。这意味着在调用 func1() 很久之后,计时器显然仍在内存中,执行其操作。我的问题是,如果我将其添加到 func2() 中:

void func2(object a, EventArgs b) {
    // Called every 5 seconds once func1() is called

    ((DispatcherTimer)a).Stop()
}

计时器会很快被垃圾回收拾取,还是会继续保留在内存中直到程序退出?如果它保留在内存中,我如何将其标记为手动收集(或执行类似的操作)?

我的第二个问题(如果您愿意回答的话)是,常规计时器在这种情况下是否会具有完全相同的行为,或者是否存在我应该了解的显着差异。

谢谢!

Consider this pair of functions in C#:

void func1() {
    DispatcherTimer tmr = new DispatcherTimer();
    tmr.Interval = TimeSpan.FromSeconds(5);
    tmr.Tick += func2;
    tmr.Start();
}

void func2(object a, EventArgs b) {
    // Called every 5 seconds once func1() is called
}

After calling func1() once, func2() is called every 5 seconds from then on, even though I lose the reference to my timer since its scope is restricted to func1(). That means that the timer is obviously still in memory, doing its thing, long after func1() was called. My question is, if I add this to func2():

void func2(object a, EventArgs b) {
    // Called every 5 seconds once func1() is called

    ((DispatcherTimer)a).Stop()
}

will the timer be picked up by garbage collection soon after, or will it continue to stay in memory until the program exits? If it stays in memory, how can I mark it for collection manually (or do something similar)?

A secondary question I have (if you feel inclined to answer) is if a regular Timer would have the exact same behavior in this situation or if there is a significant difference I should know about.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-10-26 01:05:57

Threading.Dispatcher 类保留所有活动 DispatcherTimers 的列表。当您在 Tick 事件处理程序中调用 Stop() 时,计时器将从该列表中删除。现在不再有任何对计时器的引用。它最终将被垃圾收集。这没关系,因为无法再次启动计时器。毕竟,您没有任何方法可以再获取引用,Tick 事件处理程序是您最后的尝试。

The Threading.Dispatcher class keeps a list of all active DispatcherTimers. When you call Stop() in the Tick event handler then the timer will be removed from that list. There are now no longer any references to the timer. It will eventually be garbage collected. Which is okay because there is no way to get the timer started again. After all, you don't have any way to get the reference anymore, the Tick event handler was your last shot at it.

回眸一遍 2024-10-26 01:05:57

根据 this,您必须调用 stop 并删除处理程序。

According to this, you'll have to call stop and remove the handler.

红ご颜醉 2024-10-26 01:05:57

基本上,DispatcherTimer 会生成一个新线程。因此它的范围不能像我们通常认为的那样定义。您也可以通过特定方法杀死线程。

Basically a DispatcherTimer spawns a new Thread.So its scope cannot be defined as normally we would think. You could alternatively kill the thread by specific methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文