Created
March 27, 2023 14:16
-
-
Save pictos/aa0285e428dbe57eaaffe2d629956a5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SharpLab tools in Run mode: | |
• value.Inspect() | |
• Inspect.Heap(object) | |
• Inspect.Stack(value) | |
• Inspect.MemoryGraph(value1, value2, …) | |
*/ | |
using System; | |
using System.Threading.Tasks; | |
Console.WriteLine("🌄"); | |
//DoSomething_Awating().FireAndForget(e => Console.WriteLine(e.Message)); | |
DoSomething_ReturningTask().FireAndForget(e => Console.WriteLine(e.Message)); | |
await Task.Delay(3000); | |
static Task DoSomething_ReturningTask() | |
{ | |
throw new Exception("My one"); | |
return Task.Delay(2000); | |
} | |
static async Task DoSomething_Awating() | |
{ | |
throw new Exception("My one"); | |
await Task.Delay(500); | |
} | |
static class TaskEx | |
{ | |
public static void FireAndForget(this Task task, Action<Exception>? errorHandler = null) | |
{ | |
task.ContinueWith(t => | |
{ | |
Console.WriteLine(t.Exception.GetType()); | |
if (t.IsFaulted && errorHandler != null) | |
errorHandler(t.Exception.InnerException); | |
}, TaskContinuationOptions.OnlyOnFaulted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can run that code from here