c# - System.Timers.Timer crashes on exception thrown -


from microsoft documentation system.timers.timer elapsed method should swallow exceptions.

the timer component catches , suppresses exceptions thrown event handlers elapsed event.

https://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

however when subscribing using async void method exception produced crashes application. see below code:

class program {     static void main(string[] args)     {         timer timer = new timer(100);         //timer.elapsed += on_elapsedsync; //a         //timer.elapsed += on_elapsedasync; //b         timer.elapsed += on_elapsedasyncvoid; //c         timer.start();          console.writeline("running...");         console.readline();     }      private static void on_elapsedsync(object sender, elapsedeventargs e)     {         console.writeline("throwing...");         throw new exception("my exception");     }      private static void on_elapsedasync(object sender, elapsedeventargs e)     {         console.writeline("throwing...");         task.run(() => throw new exception("async exception"));     }      private static async void on_elapsedasyncvoid(object sender, elapsedeventargs e)     {         console.writeline("throwing...");         await task.run(() => throw new exception("async exception"));     } } 

the lines commented , b not crash application. line commented c does.

why case?

the link provided states:

the timer component catches , suppresses exceptions thrown event handlers elapsed event. behavior subject change in future releases of .net framework. note, however, not true of event handlers execute asynchronously , include await operator (in c#) or await operator (in visual basic). exceptions thrown in these event handlers propagated calling thread, following example illustrates. more information on exceptions thrown in asynchronous methods, see exception handling (task parallel library).

since using await latter part of documentation applies:

exceptions thrown in these event handlers propagated calling thread, following example illustrates.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -