抛出异常的回调方法计时器 [英] Throwing exceptions in callback method for Timers

查看:233
本文介绍了抛出异常的回调方法计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到这个问题的答案...随时随地

I was unable to find an answer to this question anywhere...

在回调方法抛出System.Threading.Timer(或在System.Timers.Timer事件处理程序)的异常会发生什么。是传播到其上计时器创建或线程的例外是例外丢失

What happens with the exceptions thrown in the callback method for System.Threading.Timer, (or in the event handler for System.Timers.Timer). Is the exception propagated to the thread on which the timer was created or is the exception lost?

什么是定时器的回调函数中抛出一个异常的副作用?

What are the side-effects of throwing an exception within the timer's callback functions?

什么是以突出的计时器的创建线程,在工作线程(回调方法)的异常被抛出的正确方法?

What would be the right way to signalize to the timer's creation thread that the exception in the worker thread (callback method) has been thrown?

感谢您的时间。

推荐答案

异常没有被传递回调用线程。如果你希望它是,你可以添加一个块,并想出一个办法信号调用线程。如果调用线程是一个WinForms或WPF UI线程,你可以使用<$c$c>SynchronizationContext类传递到UI线程调用。否则,你可以使用一个线程安全的队列(或同步锁定),并定期检查其在其他线程。

The exception is not passed back to the calling thread. If you want it to be, you can add a catch block and figure out a way to signal the calling thread. If the calling thread is a WinForms or WPF UI thread, you can use the SynchronizationContext class to pass a call to the UI thread. Otherwise, you could use a thread-safe queue (or a sync lock) and check it periodically in the other thread.

System.Timers.Timer 将默默地吞下异常,并继续计时器(虽然这是受了框架的未来版本中改变); System.Threading.Timer 将终止程序。

System.Timers.Timer will silently swallow exceptions and continue the timer (although this is subject to change in future versions of the framework); System.Threading.Timer will terminate the program.

这篇关于抛出异常的回调方法计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆