Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 259
mjb Avatar asked Dec 15 '16 06:12

mjb


People also ask

What happens if async method is called without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Can I use await for synchronous method?

Consider using the 'await' operator to await non-blocking API calls, or 'await Task. Run(...)' to do CPU-bound work on a background thread. SynchronousAsync. The calling code will still be able to await this method, but since the task returned by the Task.

What is async await in C#?

An async keyword is a method that performs asynchronous tasks such as fetching data from a database, reading a file, etc, they can be marked as “async”. Whereas await keyword making “await” to a statement means suspending the execution of the async method it is residing in until the asynchronous task completes.

Does async await use thread pool?

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.


1 Answers

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.

like image 75
Damien_The_Unbeliever Avatar answered Sep 28 '22 10:09

Damien_The_Unbeliever