From 980e8c4e049ed1426464307c0d336170b1ea41bf Mon Sep 17 00:00:00 2001 From: Brian Johnson Date: Sat, 15 Aug 2026 15:48:15 +0200 Subject: [PATCH] Added basic async/await timings to prove how it works. --- learning/dotnet/async-lab/Program.cs | 32 ++++++++++++++++++++++ learning/dotnet/async-lab/async-lab.csproj | 11 ++++++++ 2 files changed, 43 insertions(+) create mode 100644 learning/dotnet/async-lab/Program.cs create mode 100644 learning/dotnet/async-lab/async-lab.csproj 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 + + +