Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mentaldesk/asyncvoid
Contains a utility class to allow you catch exceptions from async void methods
https://github.com/mentaldesk/asyncvoid
Last synced: about 2 months ago
JSON representation
Contains a utility class to allow you catch exceptions from async void methods
- Host: GitHub
- URL: https://github.com/mentaldesk/asyncvoid
- Owner: mentaldesk
- License: mit
- Created: 2024-04-03T09:23:16.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-03T09:49:29.000Z (9 months ago)
- Last Synced: 2024-04-04T10:39:04.808Z (9 months ago)
- Language: C#
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
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:
- https://www.jamescrosswell.dev/posts/catching-async-void-exceptions/## Installation
To get started, add `MentalDesk.AsyncVoid` to your project by running the following command:
```bash
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:
```csharp
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:
```csharp
private async void button1_Click(object sender, EventArgs e)
{
AsyncVoid.RunSafely(
async () => await SomeMethodAsync(),
ex => MessageBox.Show(ex.Message)
);
}
```