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.

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);
  }
}

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