Olivier Gagnon
Olivier Gagnon

Reputation: 36

Compiler warning about async shouldn't happen

So the following code triggers the compiler warning:

"Async function without await operator will run synchronously"

    public async Task UpsertMariaDb()
    {
        IEnumerable<Task> insertTasks = InsertSomethingDb();
        IEnumerable<Task> updateTasks = UpdateSomethingDb();

        Task.WaitAll(insertTasks.ToArray());
        Task.WaitAll(updateTasks.ToArray());
    }

My question is the following, is there something very basic I don't understand about async/await or is this just a compiler warning bug because I haven't put an explicit "await"

Upvotes: 1

Views: 325

Answers (1)

Vivien Sonntag
Vivien Sonntag

Reputation: 4629

Task.WaitAll blocks the current thread, so the warning is correct - you try to implement an async method, but it is not async as there is nothing being awaited.

You probably meant to do:

public async Task UpsertMariaDb()
{
    IEnumerable<Task> insertTasks = InsertSomethingDb();
    IEnumerable<Task> updateTasks = UpdateSomethingDb();

    await Task.WhenAll(insertTasks);
    await Task.WhenAll(updateTasks);
}

Upvotes: 9

Related Questions