[Solved] This async method lacks ‘await’ operators and will run synchronously

my program has 3 warnings of the following statement:

This async method lacks ‘await’ operators and will run synchronously.
Consider using the ‘await’ operator to await non-blocking API calls,
or ‘await Task.Run(…)’ to do CPU-bound work on a background thread.

What is the warning try to tell me? What should I do?

This is my code: Is it running using multi-threading?

static void Main(string[] args)
{
    Task task1 = new Task(Work1);
    Task task2 = new Task(Work2);
    Task task3 = new Task(Work3);

    task1.Start();
    task2.Start();
    task3.Start();

    Console.ReadKey();
}

static async void Work1()
{
    Console.WriteLine("10 started");
    Thread.Sleep(10000);
    Console.WriteLine("10 completed");
}

static async void Work2()
{
    Console.WriteLine("3 started");
    Thread.Sleep(3000);
    Console.WriteLine("3 completed");
}

static async void Work3()
{
    Console.WriteLine("5 started");
    Thread.Sleep(5000);
    Console.WriteLine("5 completed");
}
Enquirer: mjb

||

Solution #1:

The async keyword, by itself, doesn’t really do much. Remove it from your code and your code will act exactly the same.

What does async do?

  • It changes what’s valid inside of the method, specifically it allows you to use the await keyword
  • In turn, it means that the body of the method will be transformed, based on the awaits that are present in the body of the method.
  • And if the method returns a value, the method is also transformed to wrap the return value in a Task.

However, if you a) Don’t have any awaits in your method body and b) are void returning, then nothing special will be achieved. The compiler warning does try to be clear about this – an async method without any awaits just plain doesn’t make sense. awaits are the more important part of this feature.

Solution #2:

Yes, your code will probably use multi-threading. However, it would still do so if you just removed the async keyword. As you did not explain why it’s there, I suggest removing it.

If you want an async/await pattern, you can use Task.Delay() but I would suggest you read more on async/await before using it:

static async void Work3()
{
    Console.WriteLine("5 started");
    await Task.Delay(5000);
    Console.WriteLine("5 completed");
}
Respondent: nvoigt

Solution #3:

if you are overriding and async method with a sync method you can :

await Task.Run(() => [YOUR SYNC METHOD]);

Respondent: Johan

Solution #4:

You have used ‘async‘ keyword with method which indicates that Work1(),Work2() and Work3() methods are executed asynchronously,but you have not used ‘await’ keyword.So it executed as synchronously.Use ‘await‘ keyword if you want to execute it asynchronously.

 static async void Work1()
 {
     Console.WriteLine("10 started");
     await Task.Delay(10000);
     Console.WriteLine("10 completed");
 }

 static async void Work2()
 {
     Console.WriteLine("3 started");
     await Task.Delay(3000);
     Console.WriteLine("3 completed");
 }

 static async void Work3()
 {
     Console.WriteLine("5 started");
     await Task.Delay(5000);
     Console.WriteLine("5 completed");
 }
Respondent: jignesh

Solution #5:

You designated your methods (Work1, Work2, Work3) with the keyword async but none of your code within those methods uses the await operator to invoke asynchronous calls.

Respondent: Hawkmooon

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.