wpf 处置或终止调度程序计时器对象和访问调度程序计时器对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13454894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:17:18  来源:igfitidea点击:

dispose or kill Dispatcher timer object and Accessing dispatcher timer object

c#wpfdispatchertimer

提问by ahmad05

Question 1: hi, i would like to know is there a way by which i can dispose or kill the object of dispatcher timer and create a new object of same name ?

问题 1:您好,我想知道有没有一种方法可以处理或终止调度程序计时器的对象并创建一个同名的新对象?

Qurstion 2: and can i access the dispatcher timer object in some other class if it is set to Public

Qurstion 2:如果设置为Public,我可以访问其他类中的调度程序计时器对象吗

回答by Lubo

  1. You cannot dispose DispatcherTimer object. It doesn't implement IDisposable interface. You cannot explicit kill (free, destroy) objects in managed world. If you don't need the timer object any more, disable it and set reference to it to null. It will be collected later by GC. You can disable or stop the timer by setting IsEnabled = false or call timer.Stop(). The effect is the same.
  2. Yes. I suppose you have public property like this:

    public DispatcherTimer MyTimer { get; private set; }

  1. 您不能处置 DispatcherTimer 对象。它没有实现 IDisposable 接口。您不能在托管世界中明确杀死(释放、销毁)对象。如果您不再需要计时器对象,请禁用它并将对它的引用设置为 null。稍后会被 GC 收集。您可以通过设置 IsEnabled = false 或调用来禁用或停止计时器timer.Stop()。效果是一样的。
  2. 是的。我想你有这样的公共财产:

    public DispatcherTimer MyTimer { get; private set; }