Extensions for System.Threading.Tasks.Task
.
Inspired by John Thiriet's blog posts: Removing Async Void and MVVM - Going Async With AsyncCommand.
Available on NuGet: https://www.nuget.org/packages/AsyncAwaitBestPractices/
SafeFireAndForget
- An extension method to safely fire-and-forget a
Task
- Ensures the
Task
will rethrow anException
if anException
is caught inIAsyncStateMachine.MoveNext()
- An extension method to safely fire-and-forget a
WeakEventManager
- Avoids memory leaks when events are not unsubscribed
- Used by
AsyncCommand
andAsyncCommand<T>
- Usage instructions
-
Available on NuGet: https://www.nuget.org/packages/AsyncAwaitBestPractices.MVVM/
-
Allows for
Task
to safely be used asynchronously withICommand
:IAsyncCommand : ICommand
AsyncCommand : IAsyncCommand
IAsyncCommand<T> : ICommand
AsyncCommand<T> : IAsyncCommand<T>
- Available on NuGet: https://www.nuget.org/packages/AsyncAwaitBestPractices/
- Add to any project supporting .NET Standard 1.0
- Available on NuGet: https://www.nuget.org/packages/AsyncAwaitBestPractices.MVVM/
- Add to any project supporting .NET Standard 1.0
NDC Oslo 2019
Correcting Common Async Await Mistakes in .NET
Async/await is great but there are two subtle problems that can easily creep into code:
- Creating race conditions/concurrent execution (where you code things in the right order but the code executes in a different order than you expect)
- Creating methods where the compiler recognizes exceptions but you the coder never see them (making it head-scratchingly annoying to debug especially if you accidentally introduced a race condition that you can’t see)
This library solves both of these problems.
To better understand why this library was created and the problem it solves, it’s important to first understand how the compiler generates code for an async method.
And by the way, tl;dr A non-awaited Task
doesn't rethrow exceptions so use this library!
(Source: Xamarin University: Using Async and Await)
The compiler transforms an async
method into an IAsyncStateMachine
class which allows the .NET Runtime to "remember" what the method has accomplished.
(Source: Xamarin University: Using Async and Await)
The IAsyncStateMachine
interface implements MoveNext()
, a method the executes every time the await
operator is used inside of the async
method.
MoveNext()
essentially runs your code until it reaches an await
statement, then it return
s while the await
'd method executes. This is the mechanism that allows the current method to "pause", yielding its thread execution to another thread/Task.
Look closely at MoveNext()
; notice that it is wrapped in a try/catch
block.
Because the compiler creates IAsyncStateMachine
for every async
method and MoveNext()
is always wrapped in a try/catch
, every exception thrown inside of an async
method is caught!
Now we see that the async
method catches every exception thrown - that is to say, the exception is caught internally by the state machine, but you the coder will not see it. In order for you to see it, you'll need to rethrow the exception to surface it in your debugging. So the questions is - how do I rethrow the exception?
There are a few ways to rethrow exceptions that are thrown in an async
method:
- Use the
await
keyword (Prefered)- e.g.
await DoSomethingAsync()
- e.g.
- Use
.GetAwaiter().GetResult()
- e.g.
DoSomethingAsync().GetAwaiter().GetResult()
- e.g.
The await
keyword is preferred because await
allows the Task
to run asynchronously on a different thread, and it will not lock-up the current thread.
Never, never, never, never, never use .Result
or .Wait()
:
-
Both
.Result
and.Wait()
will lock-up the current thread. If the current thread is the Main Thread (also known as the UI Thread), your UI will freeze until theTask
has completed. -
.Result
or.Wait()
rethrow your exception as aSystem.AggregateException
, which makes it difficult to find the actual exception.
An extension method to safely fire-and-forget a Task
:
public static async void SafeFireAndForget(this System.Threading.Tasks.Task task, bool continueOnCapturedContext = false, System.Action<System.Exception> onException = null)
void HandleButtonTapped(object sender, EventArgs e)
{
// Allows the async Task method to safely run on a different thread while not awaiting its completion
// onException: If an Exception is thrown, print it to the Console
ExampleAsyncMethod().SafeFireAndForget(onException: ex => Console.WriteLine(ex));
// HandleButtonTapped continues execution here while `ExampleAsyncMethod()` is running on a different thread
// ...
}
async Task ExampleAsyncMethod()
{
await Task.Delay(1000);
}
void InitializeSafeFireAndForget()
{
// Initialize SafeFireAndForget
// Only use `shouldAlwaysRethrowException: true` when you want `.SafeFireAndForget()` to always rethrow every exception. This is not recommended, because there is no way to catch an Exception rethrown by `SafeFireAndForget()`; `shouldAlwaysRethrowException: true` should **not** be used in Production/Release builds.
SafeFireAndForgetExtensions.Initialize(shouldAlwaysRethrowException: false);
// SafeFireAndForget will print every exception to the Console
SafeFireAndForgetExtensions.SetDefaultExceptionHandling(ex => Console.WriteLine(ex));
}
void UninitializeSafeFireAndForget()
{
// Remove default exception handling
SafeFireAndForgetExtensions.RemoveDefaultExceptionHandling()
}
void HandleButtonTapped(object sender, EventArgs e)
{
// Allows the async Task method to safely run on a different thread while not awaiting its completion
// onException: If a WebException is thrown, print its StatusCode to the Console. **Note**: If a non-WebException is thrown, it will not be handled by `onException`
// Because we set `SetDefaultExceptionHandling` in `void InitializeSafeFireAndForget()`, the entire exception will also be printed to the Console
ExampleAsyncMethod().SafeFireAndForget<WebException>(onException: ex =>
{
if(ex.Response is HttpWebResponse webResponse)
Console.WriteLine($"Status Code: {webResponse.StatusCode}");
});
// HandleButtonTapped continues execution here while `ExampleAsyncMethod()` is running on a different thread
}
async Task ExampleAsyncMethod()
{
await Task.Delay(1000);
throw new WebException();
}
An event implementation that enables the garbage collector to collect an object without needing to unsubscribe event handlers.
Inspired by Xamarin.Forms.WeakEventManager.
readonly WeakEventManager _canExecuteChangedEventManager = new WeakEventManager();
public event EventHandler CanExecuteChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}
void OnCanExecuteChanged() => _canExecuteChangedEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));
readonly WeakEventManager _propertyChangedEventManager = new WeakEventManager();
public event PropertyChangedEventHandler PropertyChanged
{
add => _propertyChangedEventManager.AddEventHandler(value);
remove => _propertyChangedEventManager.RemoveEventHandler(value);
}
void OnPropertyChanged([CallerMemberName]string propertyName = "") => _propertyChangedEventManager.HandleEvent(this, new PropertyChangedEventArgs(propertyName), nameof(PropertyChanged));
readonly WeakEventManager _weakActionEventManager = new WeakEventManager();
public event Action ActionEvent
{
add => _weakActionEventManager.AddEventHandler(value);
remove => _weakActionEventManager.RemoveEventHandler(value);
}
void OnActionEvent(string message) => _weakActionEventManager.HandleEvent(message, nameof(ActionEvent));
An event implementation that enables the garbage collector to collect an object without needing to unsubscribe event handlers, inspired by Xamarin.Forms.WeakEventManager.
readonly WeakEventManager<string> _errorOcurredEventManager = new WeakEventManager<string>();
public event EventHandler<string> ErrorOcurred
{
add => _errorOcurredEventManager.AddEventHandler(value);
remove => _errorOcurredEventManager.RemoveEventHandler(value);
}
void OnErrorOcurred(string message) => _errorOcurredEventManager.HandleEvent(this, message, nameof(ErrorOcurred));
readonly WeakEventManager<string> _weakActionEventManager = new WeakEventManager<string>();
public event Action<string> ActionEvent
{
add => _weakActionEventManager.AddEventHandler(value);
remove => _weakActionEventManager.RemoveEventHandler(value);
}
void OnActionEvent(string message) => _weakActionEventManager.HandleEvent(message, nameof(ActionEvent));
Allows for Task
to safely be used asynchronously with ICommand
:
AsyncCommand<T> : IAsyncCommand<T>
IAsyncCommand<T> : ICommand
AsyncCommand : IAsyncCommand
IAsyncCommand : ICommand
public AsyncCommand(Func<T, Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false)
public AsyncCommand(Func<Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
bool continueOnCapturedContext = false)
public class ExampleClass
{
public ExampleClass()
{
ExampleAsyncCommand = new AsyncCommand(ExampleAsyncMethod);
ExampleAsyncIntCommand = new AsyncCommand<int>(ExampleAsyncMethodWithIntParameter);
ExampleAsyncExceptionCommand = new AsyncCommand(ExampleAsyncMethodWithException, onException: ex => Console.WriteLine(ex.ToString()));
ExampleAsyncCommandReturningToTheCallingThread = new AsyncCommand(ExampleAsyncMethod, continueOnCapturedContext: true);
}
public IAsyncCommand ExampleAsyncCommand { get; }
public IAsyncCommand<int> ExampleAsyncIntCommand { get; }
public IAsyncCommand ExampleAsyncExceptionCommand { get; }
public IAsyncCommand ExampleAsyncCommandReturningToTheCallingThread { get; }
async Task ExampleAsyncMethod()
{
await Task.Delay(1000);
}
async Task ExampleAsyncMethodWithIntParameter(int parameter)
{
await Task.Delay(parameter);
}
async Task ExampleAsyncMethodWithException()
{
await Task.Delay(1000);
throw new Exception();
}
void ExecuteCommands()
{
ExampleAsyncCommand.Execute(null);
ExampleAsyncIntCommand.Execute(1000);
ExampleAsyncExceptionCommand.Execute(null);
ExampleAsyncCommandReturningToTheCallingThread.Execute(null);
}
}