Skip to content

mentaldesk/asyncvoid

Repository files navigation

MentalDesk.AsyncVoid

This package allows you to catch exceptions from async void methods, rather than let them crash your application.

For a full explanation, see:

Installation

To get started, add MentalDesk.AsyncVoid to your project by running the following command:

dotnet add package MentalDesk.AsyncVoid

Usage

Most typically, you'd use this if you needed to run some asynchronous code from within an event handler in a UI application (such as WinForms, WinUI or UWP). In that case, you'd need to change the event handler to be an async void :-(

So you might have some code like this for example:

private async void button1_Click(object sender, EventArgs e)
{
    await SomeMethodAsync();
}

If SomeMethodAsync throws an exception, it will crash your application. To avoid this, the code can be modified to call SomeMethodAsync using the utility class from this package:

private async void button1_Click(object sender, EventArgs e)
{
    AsyncVoid.RunSafely(
        async () => await SomeMethodAsync(), 
        ex => MessageBox.Show(ex.Message)
    );
}

About

Contains a utility class to allow you catch exceptions from async void methods

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages