DispatcherTimer 停止运行了?

来源:互联网 发布:淘宝拍摄场景布置 编辑:程序博客网 时间:2024/05/01 06:16

最近在项目中 Fix 了一个和 DispatcherTimer 有关的 Bug:程序中有一个 DispatcherTimer,每隔一段时间(例如 1s)执行一次,主要就是为了获得当前的时间,然后根据时间做一些更新。但是在某些时刻,该 DispatcherTimer 停止运行了。

在解释原因之前先解释以下几个概念:

第一个概念是关于 WPF 中的 DispatcherTimer。

WPF 中的 DispatcherTimer 和 Thread 里 Timer 相似但是又有本质的不同。说相似是因为它们的作用相同,都是每间隔一段时间(Interval)执行一次对应的方法(Tick)。说不同是因为,DispatcherTimer 是运行在 UI 线程上的,而 Timer 是运行于其他线程。

DispatcherTimer 不能保证在正好在时间间隔 (Interval)执行对应的方法(Tick),只能保证不会在 Interval 之前执行。这是因为 DispatcherTimer 方法是放置在 Dispatcher 队列中,是和 UI 相同的线程上。(如果想验证的话,可以在 DispatcherTimer 的 Tick 方法中打印 Thread.CurrentThread.ManagedThreadId,并与 UI 线程上的 Thread.CurrentThread.ManagedThreadId 来进行比较)。

Timer 对象运行在不同于 UI 线程的其他线程上,这就是说如果需要在 Tick 方法中访问界面上的元素,需要调用 Application.Current.Dispatcher.BeginInvoke 或 Application.Current.Dispatcher.Invoke 方法。

第二个就是关于 WPF 中的 Exception Handler。

在做 WPF 应用程序时,我们经常会在全局的地方进行 Exception Handler:

public App(){    this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);}private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e){    e.Handled = true;}

如上所示,对 Application 的 DispatcherUnhandledException 加上 Handler 之后,每当在系统在 UI 线程发生没有被处理的 Exception 时,都会运行 App_DispatcherUnhandledException 方法中,在该方法中将 e.Handled = true,表示该 Exception 被处理的,通俗点说就是该 Exception 被程序吃掉了,不会导致程序 Crash 了。

概念讲完了,下面说下出现问题的原因了。简单点说就是在那个时间点上,在 Tick 方法中发生了 Exception,最终导致当前的 DispatcherTimer 停止运行了!

我们可以写一个小的 Demo 来测试下:

int count = 0;DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };timer.Tick += (sender, e) =>{    if (count == 5)    {        throw new Exception();    }    Trace.WriteLine(count++);};timer.Start();

运行时,可以发现,当 Tick 方法中出现 Exception 后,该 DispatcherTimer 就被停止了。

Solution 1:不要在 Tick 方法中出现 Exception,但是由于需求的限制,这个暂时是没有办法完成的。

Solution 2:创建一个守护的 Timer,发现 DispatcherTimer 关掉后,重新打开 DispatcherTimer。(没有尝试过,但是应该可以。)

Solution 3:。。。。。

条条大路通罗马。出现这个问题的主要原因是由于在 Tick 方法中出现了 Exception,反过来说,只要在 Tick 方法中没有 Exception,那这个 Bug 就不存在了。所以我就在 Tick 方法中,增加一个 try...catch...方法,将 Exception 吃掉了大笑。当然,这个 Solution 对当前的程序来说是可以接受的,但并不是适用于所有的程序。代码如下所示:

int count = 0;DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };timer.Tick += (sender, e) =>{    try    {        if (count == 5)        {            throw new Exception();        }    }    catch    {    }    Trace.WriteLine(count++);};



0 0