Skip to content

Instantly share code, notes, and snippets.

@yuessir
Created August 13, 2018 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuessir/d636f67e7e7843557eba0b90876793d8 to your computer and use it in GitHub Desktop.
Save yuessir/d636f67e7e7843557eba0b90876793d8 to your computer and use it in GitHub Desktop.
// shared variables
// private static ThreadLocal<int> local = new ThreadLocal<int>();//output 53000 0;
private static AsyncLocal<int> local = new AsyncLocal<int>();//output 53000 53000;
private static void Main(string[] args)
{
// declare a delegate
Action act = async () =>
{
await RunAsync();
};
// action delegate
act();
Console.Read();
}
private static async Task RunAsync()
{
local.Value = 53000;//set values
Console.WriteLine($"Before await:{nameof(local)} = {local.Value}");
await Task.Delay(50); //await 50 miniseconds
Console.WriteLine($"After await:{nameof(local)} = {local.Value}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment