Nick Williams
Nick Williams

Reputation: 1267

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?

Upvotes: 0

Views: 2244

Answers (1)

mjwills
mjwills

Reputation: 23923

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.

Upvotes: 4

Related Questions