0% found this document useful (0 votes)
16 views2 pages

Await in C#

The document explains the behavior of the 'await' keyword in asynchronous programming, clarifying that it pauses the method execution without blocking the thread. It uses a coffee shop analogy to illustrate the difference between blocking and non-blocking waits. For true parallel execution, it suggests using 'Task.Run()' to run tasks in the background.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Await in C#

The document explains the behavior of the 'await' keyword in asynchronous programming, clarifying that it pauses the method execution without blocking the thread. It uses a coffee shop analogy to illustrate the difference between blocking and non-blocking waits. For true parallel execution, it suggests using 'Task.Run()' to run tasks in the background.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Clarified Truth:

❗ await tells the compiler:


“Pause this method here, and when the awaited task finishes, resume execution from here.
Don’t block the thread.”

It’s not that the rest of the method keeps going while the task runs. The method stops at await
and resumes later, after the task finishes.

So it's not like “run in background and keep going.”


It’s more like: “do something async, wait non-blockingly, then resume.”

🎯 Visual Analogy
Imagine you’re in a coffee shop.

Without await:

You order a coffee and stand at the counter for 5 minutes doing nothing, waiting. ☕ (Thread is
blocked)

With await:

You order a coffee and then go sit and read a book while waiting. When the barista calls your
name, you go get your coffee and continue. 📖➡️☕ (Thread is free, method resumes later)

🔄 Behind the Scenes


public async Task<string> GetDataAsync()
{
await Task.Delay(2000); // Simulates a delay
return "Data";
}

What happens:

1. The method starts.


2. It hits await Task.Delay(2000).
3. It returns control to the caller, but remembers where it paused.
4. After 2 seconds, it resumes from the next line.
The task is not running on a new thread by default.
It just uses the event loop to continue later—very lightweight.

✅ So to be super clear:
Myth Reality
await continues method and "picks up result ❌ Not true — it pauses method until result is
later" ready
await runs on a new thread ❌ No — unless you explicitly use Task.Run()
await means non-blocking pause, then resume
✅ Correct ✔️
later

If You Want Real Parallelism


If you want the method to run in the background and not pause at all, then you'd write:

public void FireAndForget()


{
var _ = Task.Run(async () =>
{
var result = await GetDataAsync(); // This runs in background
Console.WriteLine(result);
});

Console.WriteLine("I didn’t wait for result");


}

TL;DR:
 await does pause the method, but doesn't block the thread.
 It's not the same as launching a task in the background.
 If you want true parallel background execution, use Task.Run().

You might also like