Created
July 22, 2021 09:52
-
-
Save mgravell/ba74399609714fbfa03f7f51bab00925 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Runtime.CompilerServices; | |
using System.Threading; | |
using System.Threading.Tasks; | |
static class P | |
{ | |
static Task Main() => A(); | |
static async Task A() | |
{ | |
Show(); | |
Set(); | |
Show(); | |
await B(); | |
Show(); | |
} | |
static async Task B() | |
{ | |
Show(); | |
Set(); | |
await Task.Delay(100); | |
// this one rolls back because of the await | |
Show(); // B | |
await C(); | |
Show(); // B | |
// this one doesn't roll back because there is no await | |
Show(); // B | |
D(); | |
Show(); // D | |
} | |
static async Task C() | |
{ | |
Show(); | |
Set(); | |
Show(); | |
} | |
static void D() | |
{ | |
Show(); | |
Set(); | |
Show(); | |
} | |
static void Set([CallerMemberName] string value = default) | |
=> s.Value = value; | |
static void Show([CallerMemberName] string caller = default, [CallerLineNumber] int line = default) | |
=> Console.WriteLine($"[{caller}#{line}]: {s.Value}"); | |
static readonly AsyncLocal<string> s = new(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment