I use the same code below for the non-critical "reboot and crash" operation in ASP.NET
private void SomeMethod()
{
FireAndForgetAsync();
}
private async Task FireAndForgetAsync()
{
await Task.Delay(sleepTime).ConfigureAwait(false);
throw new Exception("An Error Occurred");
}
The exception is caught in the task returned by the async method, which prevents the unhandled exception from completely killing the application.
But will this code cause memory leaks? Because the returned task (and its exception) is not assigned / not available? My thoughts is that it will just be garbage collection if some .NET code does not contain a reference to the task.
source
share