Throwing exceptions in callback method for Timers

后端 未结 4 1613
醉酒成梦
醉酒成梦 2020-12-01 15:47

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

What happens with the exceptions thrown in the callback method for System.Threading.Timer, (or in the eve

4条回答
  •  借酒劲吻你
    2020-12-01 16:15

    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 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.

提交回复
热议问题