MSDN Documentation

Home

Asynchronous Programming in .NET

Asynchronous programming enables applications to remain responsive while performing long-running operations such as I/O, network calls, or CPU‑bound work. The .NET Framework provides the Task‑based Asynchronous Pattern (TAP) and the async/await keywords to simplify this model.

Key Concepts

Task‑Based Asynchronous Pattern (TAP)

TAP standardizes method signatures for asynchronous operations:

// Example of a TAP method
public Task<int> GetDataAsync(CancellationToken token = default)
{
    return Task.Run(() =>
    {
        // Simulate work
        Thread.Sleep(2000);
        return 42;
    }, token);
}

Using async and await

The async modifier enables the await keyword inside a method. The compiler rewrites the method into a state machine.

public async Task ProcessAsync()
{
    var data = await GetDataAsync();
    Console.WriteLine($"Result: {data}");
}

Best Practices

Common Pitfalls

Further Reading