Skip to main content

CRR0037 - Task.Wait is used in an async method

This analyzer detects blocking task.Wait methods with the new async/await pattern. Use the new await-friendly methods instead of blocking methods:

Blocking Await-friendly
task.Wait, task.Result await task
Task.WaitAny await Task.WhenAny
Task.WaitAll await Task.WhenAll
Thread.Sleep await Task.Delay

Refer to the Task-based Asynchronous Pattern (TAP) MSDN article for more information.

async Task<string> MethodNameAsync(int value, CancellationToken token) {
    token.ThrowIfCancellationRequested();
    var tasks = new List<Task>();
    for (int i = 0; i < 10; i++)
        tasks.Add(Task.Delay(100));
    Task.WaitAll(tasks.ToArray());  // CRR0037
    return value.ToString();
}

Replace the Task.WaitAll call with await Task.WhenAll to fix this issue.

async Task<string> MethodNameAsync(int value, CancellationToken token) {
    token.ThrowIfCancellationRequested();
    var tasks = new List<Task>();
    for (int i = 0; i < 10; i++)
        tasks.Add(Task.Delay(100));
    await Task.WhenAll(tasks.ToArray());
    return value.ToString();
}