为什么Timer没有执行回调?

时间:2016-12-25 13:41:52

标签: c# timer

有一个计时器,它的回调必须执行一次:

var timer = new Threading.Timer(callback: MyCallback, state: NotNullObject, dueTime: Timeout.Infinite, period: Timeout.Infinite);
//note state is some not-null object
//timer is local variable 

//there is some code...
timer.Change(new TimeSpan(0, 0, 5), Timeout.InfiniteTimeSpan); //launch timer in 5 seconds

根据this answer,如果state参数不为null,则垃圾收集器不会收集计时器。那么为什么MyCallback永远不会在这里执行呢?

还有另一个相关的问题:如果state参数不为null并且只需一次操作就需要定时器,那是否意味着GC以后不能收集此定时器?即在这种情况下是不必要的内存消耗?

0 个答案:

没有答案
相关问题