I'm absolutely down with the whole 'all the way down' philosophy of async programming. However, there are many places in .Net where you just don't have any control over the calling signature. Event handlers come to mind. Event handlers have no return type (they're events, after all):
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
Task.Run(async () => await Task.Delay(0).ConfigureAwait(false));
}
If I use this pattern, I get flagged by the analyzer that I should observe the awaitable by using an await. That's not an option in a handler. So I tried this pattern:
protected virtual void OnThresholdReached(EventArgs e)
{
Task.Run(async () => await Task.Delay(0).ConfigureAwait(false)).Wait();
}
And I'm told that waiting on a synchronous task can cause a deadlock. There's a suggestion to use the 'JoinableTaskFactory.Run', but that's in a Visual Studio package and I don't what to bring in an additional package for something that should be a core method.
So what is the best practice for handling a long running task in an event handler?