简体   繁体   中英

System.Timers.Timer crashes on exception thrown

From the Microsoft documentation the System.Timers.Timer elapsed method should swallow all exceptions.

The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event.

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

However when subscribing using an async void method an exception is produced which crashes the application. See the 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 A and B do not crash the application. The line commented C does.

Why is this the case?

The link you provided states:

The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework. Note, however, that this is not true of event handlers that execute asynchronously and include the await operator (in C#) or the Await operator (in Visual Basic). Exceptions thrown in these event handlers are propagated back to the calling thread, as the following example illustrates. For more information on exceptions thrown in asynchronous methods, see Exception Handling (Task Parallel Library).

Since you are using await then the latter part of the documentation applies:

Exceptions thrown in these event handlers are propagated back to the calling thread, as the following example illustrates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM