الانتقال إلى المحتوى
.NETC#· 7 دقائق للقراءة

async/await Doesn't Create Threads: What Really Happens

An async method runs on your thread until it has to wait. Where the waiting goes, and where the threads really come from.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

A ticket says the desktop app freezes for several seconds every time someone clicks Export. The fix merged last sprint looked convincing: BuildReport became async Task<string> BuildReportAsync, the click handler now awaits it, and the pull request says "moved report generation to a background thread." The window still freezes exactly as long as before. Nothing moved anywhere. The method still hashes and formats the export on the UI thread, because async never asked for another thread in the first place.

The common misconception

The belief goes like this: marking a method async makes it run in the background, on a new or separate thread, so the caller is never held up. Calling it starts a parallel job, and await waits for that job to finish.

It's an easy model to arrive at. "Asynchronous" sounds like "at the same time." Before async and await, the usual way to keep a UI responsive was a BackgroundWorker or a Thread, so asynchrony and extra threads came as a pair. Tutorials often show async next to Task.Run, which really does use another thread. And if you log thread IDs in a console app, the code after an await often runs on a different thread than the code before it, which looks like proof.

But Microsoft's docs say it plainly: the async and await keywords don't cause extra threads to be created. The thread you see after an await was borrowed, and the code before the first real wait never left the caller's thread.

What actually happens

async changes how the method is compiled

async is an instruction to the compiler, not to a scheduler. It allows await in the body, and the compiler rewrites the method as a state machine: a small generated type that keeps the locals the method needs later and remembers which await it reached. Calling the method starts that state machine right away, on the calling thread, like any other call. It keeps running there until it awaits something that isn't finished yet. A method that never reaches such an await runs synchronously from start to end, whatever its signature says.

await is a point where the method may pause

At each await, the method first checks whether the awaited task has already completed. If it has, the method takes the result and keeps going without pausing. If not, it registers the rest of the method as a , saves its state, and returns. In Release builds this is the moment the state machine, a struct until now, is copied to the heap. The caller receives an unfinished Task and carries on, or awaits it and returns to its own caller in turn. On a UI thread, that return is what lets the window keep processing clicks and repainting.

While it waits, no thread waits with it

Suppose the awaited operation is an HTTP request or a database query. Once the request is on its way, there's nothing to execute until the response arrives, so no thread of yours sits blocked on it. That's the whole point on a server: ASP.NET Core can serve thousands of concurrent requests with a small pool of threads, because those threads aren't parked waiting for I/O.

Where the rest of the method runs

When the awaited task completes, the continuation needs some thread to run on. By default, await captures the current . WPF and WinForms install one that posts the continuation back to the UI thread, so the code after an await can update controls safely. ASP.NET Core and console apps have none, so the continuation usually runs as part of completing the task, on a thread:

C# Program.cs
Console.WriteLine($"Before: thread {Environment.CurrentManagedThreadId}");await Task.Delay(500);   // no thread is blocked during these 500 msConsole.WriteLine($"After:  thread {Environment.CurrentManagedThreadId}");// In a console app the two IDs usually differ: the pool lent a thread

Over its lifetime, one async method can run on several threads, one after another, but never on two at once. None of those threads exists because you wrote async.

Real threads come from somewhere else

Back to the Export button. Hashing and formatting are CPU-bound: they need a CPU, and so a thread, for every millisecond they run. async can't hand that work to another thread; you have to ask for one. Here's a console version of the same code, before and after:

C# Program.cs
using System.Security.Cryptography;var data = new byte[200_000_000];Task<string> export = ExportAsync(data);Console.WriteLine("Back in the caller"); // printed only after the hashingawait export;static async Task<string> ExportAsync(byte[] data){    // async moved nothing: this runs on the caller's thread    string hash = Convert.ToHexString(SHA256.HashData(data));    await File.WriteAllTextAsync("export.sha256", hash); // first await    return hash;}

In the first version, the hashing runs on the caller's thread before ExportAsync reaches its first await, so "Back in the caller" appears only when all the work is done. In the second, Task.Run queues the hashing to the thread pool, and the await on that unfinished task returns control at once. In the desktop app, that one change is what unfreezes the window.

Myth vs reality

MythReality
async runs the method on a background threadIt runs on the caller's thread until the first await that has to wait
await blocks until the task finishesIt hands an unfinished Task to the caller and frees the thread
Waiting for I/O ties up a threadA pending network or database call blocks no thread while it waits
A different thread after await means async created itThe continuation borrowed a pool thread, or went back to the UI thread
Adding async makes CPU-heavy code non-blockingCPU work needs a thread; in a UI app, ask for one with Task.Run

How to apply it in your project

  1. Find async methods that never really waitLook for async methods with no await, or whose only await comes after the expensive part. Everything up to that point runs on the caller's thread. Make the slow part truly asynchronous, or treat the method as the synchronous method it is.
  2. Classify each slow operationWaiting on a network, disk or database is I/O-bound: call the real async API, such as GetStringAsync, ToListAsync or ReadAsync, and await it without Task.Run. Computing something is CPU-bound: in a UI app, wrap it in Task.Run at the call site, the event handler, rather than inside a library method.
  3. Don't tie state to a threadAfter an await, the code may continue on a different thread. Don't keep per-request data in [ThreadStatic] fields or assume the thread ID stays the same. Pass data as parameters, or use AsyncLocal<T> when you really need ambient data.
  4. Await instead of blocking.Result and .Wait() turn a freed thread back into a blocked one, and on a UI thread they can deadlock. Await all the way up to the event handler or the endpoint.

Key takeaways

  • async lets a method use await; it doesn't create, request or switch threads.
  • An async method runs synchronously on the caller's thread until the first await that has to wait, then returns an unfinished Task.
  • While an I/O operation is pending, no thread is blocked on it, which is how a few threads can serve many requests.
  • The code after an await runs where the captured context sends it: the UI thread, or a thread pool thread when there's no context.
  • CPU-bound work needs a thread you ask for, with Task.Run in UI apps; in ASP.NET Core, Task.Run plus await only adds overhead.

📚 المصادر

  1. The Task Asynchronous Programming (TAP) model with async and await — Microsoft Learn
  2. Asynchronous programming with async and await — Microsoft Learn
  3. How Async/Await Really Works in C# — Stephen Toub, .NET Blog
  4. ConfigureAwait FAQ — Stephen Toub, .NET Blog
  5. ASP.NET Core Best Practices: Avoid blocking calls — Microsoft Learn

التعليقات (0)