Having the next piece of code, why do the following signatures for an event behave differently?
This one throws the exception:
public event Action SomeEvent;
This one does not throw the exception:
public event Func<Task> SomeEvent;
This is the code for a console application. Using the Action variant for declaring the event stops the application and shows the stack trace.
internal static class Program
{
private static void Main()
{
var watcher = new Watcher();
watcher.Context.RaiseSomeEvent();
Console.ReadKey();
}
}
internal class Watcher
{
public Context Context { get; } = new Context();
public Watcher()
{
Context.SomeEvent += async () =>
{
Console.WriteLine($"Sleeping, current thread {Thread.CurrentThread.ManagedThreadId}");
await Task.Run(() => Thread.Sleep(1000));
Console.WriteLine($"Slept, current thread {Thread.CurrentThread.ManagedThreadId}");
throw new Exception();
};
}
}
internal class Context
{
public event Action SomeEvent;
//public event Func<Task> SomeEvent;
public void RaiseSomeEvent()
{
SomeEvent?.Invoke();
}
}
I had some reading such as this but I did't found anything specific to events.