Skip to content

Instantly share code, notes, and snippets.

@mgravell
Created July 22, 2021 09:52
Show Gist options
  • Save mgravell/ba74399609714fbfa03f7f51bab00925 to your computer and use it in GitHub Desktop.
Save mgravell/ba74399609714fbfa03f7f51bab00925 to your computer and use it in GitHub Desktop.
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