An async method runs synchronously until it reaches an incomplete await. It then returns a Task and schedules the remainder as a continuation. Await does not automatically create a thread; genuine asynchronous I/O can wait without occupying an application thread. Example: public async Task<Order?> FindAsync(Guid id, CancellationToken ct) { return await db.Orders.AsNoTracking() .SingleOrDefaultAsync(x => x.Id == id, ct); } Use async all the way, pass cancellation, and avoid .Result or .Wait(), which block threads and can cause deadlocks in context-bound applications or thread-pool starvation in servers. Return Task rather than async void except for event handlers.
Backbencher Developer · · 0 likes