1

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"

  • 2
    You should use the `await` keyword in a method marked as `async`. If you are just calling `Task.Wait...` you don't need to mark the method as `async`. – rhughes Jun 18 '20 at 18:16
  • 3
    Did you mean to use `await Task.WhenAll()` (asynchronous) instead of `Task.WaitAll()` (synchronous)? – itsme86 Jun 18 '20 at 18:17

1 Answers1

9

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);
}
Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49