0Pricing
C# Academy · Lesson

Async pitfalls: sync-over-async and deadlocks

Identify async pitfalls such as sync-over-async and deadlocks. Learn why blocking calls can freeze apps, and how to avoid common traps.

Async pitfalls: sync-over-async and deadlocks is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Pitfalls overview

Pitfalls: Async makes coding easier, but mixing sync and async can freeze apps or cause deadlocks.

Sync-over-async

Calling .Result on a Task blocks the thread. In console apps this may run, but in UI/ASP.NET contexts it risks a deadlock.

using System;
using System.Threading.Tasks;

public class Program
{
  static async Task<string> SayHelloAsync()
  {
    await Task.Delay(200); // simulate async delay
    return "Hello";
  }

  public static void Main(string[] args)
  {
    // BAD: calling .Result blocks the thread
    string msg = SayHelloAsync().Result;
    Console.WriteLine(msg);
  }
}

Safer completion

In console demos you can block carefully. In real apps, prefer async all the way: mark methods async and use await, not .Result.

using System;
using System.Threading.Tasks;

public class Program
{
  static async Task<string> SayHelloAsync()
  {
    await Task.Delay(200);
    return "Hello async";
  }

  public static void Main(string[] args)
  {
    // In C# 6, Main is sync; avoid blocking in real apps
    string msg = SayHelloAsync().GetAwaiter().GetResult(); // safer than .Result
    Console.WriteLine(msg);
  }
}

Deadlock mechanism

Deadlock chain:

  • Thread blocks on Task.Result
  • Continuation needs that same thread
  • Continuation never runs
  • Result never arrives — a deadlock

Wait() issue

.Wait() is another blocking call that can cause the same problems as .Result.

using System;
using System.Threading.Tasks;

public class Program
{
  static async Task<string> WorkAsync()
  {
    await Task.Delay(100);
    return "done";
  }

  public static void Main(string[] args)
  {
    // BAD: Wait() can also deadlock in UI/ASP.NET
    WorkAsync().Wait();
    Console.WriteLine("Completed");
  }
}

Best practices

Guidelines:

  • Avoid mixing sync and async
  • Use async/await through the whole call stack
  • Block only in demo console apps
  • In UI or web apps, never call .Result or .Wait()

Result deadlock risk

Quick check: Why is calling .Result on an async Task dangerous in UI or ASP.NET contexts?

Recap

Recap: Blocking async Tasks with .Result or .Wait() can cause deadlocks. Prefer async/await all the way.

Frequently asked questions

Is the “Async pitfalls: sync-over-async and deadlocks” lesson free?

Yes — the full text of “Async pitfalls: sync-over-async and deadlocks” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Async pitfalls: sync-over-async and deadlocks”?

Identify async pitfalls such as sync-over-async and deadlocks. Learn why blocking calls can freeze apps, and how to avoid common traps. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Async pitfalls: sync-over-async and deadlocks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Task/ValueTask and the async state machine (concepts)
  2. Async pitfalls: sync-over-async and deadlocks
  3. ConfigureAwait, exception flow
← Back to C# Academy