diff --git a/learning/dotnet/async-lab/Program.cs b/learning/dotnet/async-lab/Program.cs new file mode 100644 index 0000000..2026d46 --- /dev/null +++ b/learning/dotnet/async-lab/Program.cs @@ -0,0 +1,32 @@ +using System.Diagnostics; + +long startTime = Stopwatch.GetTimestamp(); + +Console.WriteLine( + $"[Program] start - thread {Environment.CurrentManagedThreadId}, t = {Stopwatch.GetElapsedTime(startTime).TotalMilliseconds} ms" +); + +var task = FetchUserAsync(42); +Console.WriteLine( + $"[Program] after fetch - thread {Environment.CurrentManagedThreadId}, t = {Stopwatch.GetElapsedTime(startTime).TotalMilliseconds} ms" +); + +var user = await task; +Console.WriteLine( + $"[Program] after await - thread {Environment.CurrentManagedThreadId}, t = {Stopwatch.GetElapsedTime(startTime).TotalMilliseconds} ms" +); + +return; + +async Task FetchUserAsync(int id) +{ + Console.WriteLine( + $"[FetchUserAsync] entered - thread {Environment.CurrentManagedThreadId}, t = {Stopwatch.GetElapsedTime(startTime).TotalMilliseconds} ms" + ); + await Task.Delay(500); + Console.WriteLine( + $"[FetchUserAsync] resumed, thread {Environment.CurrentManagedThreadId}, t = {Stopwatch.GetElapsedTime(startTime).TotalMilliseconds} ms" + ); + + return $"User ID: {id}"; +} diff --git a/learning/dotnet/async-lab/async-lab.csproj b/learning/dotnet/async-lab/async-lab.csproj new file mode 100644 index 0000000..42b22b1 --- /dev/null +++ b/learning/dotnet/async-lab/async-lab.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + async_lab + enable + enable + + +