2

In the following program:

using System;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static async Task f()
    {
        void action()
        {    
            Thread.Sleep(100);
            Console.WriteLine("4");
            Thread.Sleep(100);
        }
        await Task.Run(action);
    }
    static async void g()
    {
        Thread.Sleep(100);
        Console.WriteLine("1");
        Console.WriteLine("2");
        await f();
        Console.WriteLine("5");
    }
    static void Main()
    {
        g();
        Console.WriteLine("3");
        Console.ReadKey();
    }
}

I can change the function f and write it as:

static Task f()
{
    void action()
    {
        Thread.Sleep(100);
        Console.WriteLine("4");
        Thread.Sleep(100);
    }
    return Task.Run(action);
}

The output doesn't change. Which is preferred? an async f with await or a normal f that returns a Task?

Edit: The output for both cases:

 1
 2
 3
 4
 5
Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
  • 2
    `async void g` is a bug. It can't be awaited. Use `async Task` for methods that don't return any results. `await` *awaits* an already active asynchronous operation to complete without blocking the caller. You can't compare `await` with a method returning `Task` because they simply don't do the same things. You use `await` to await the task started by the method that returned the task – Panagiotis Kanavos Jan 20 '21 at 13:05
  • @PanagiotisKanavos Can you correct the code for me? – Minimus Heximus Jan 20 '21 at 13:07
  • In your example `the output doesn't change` isn't true. If you didn't block the main thread with `ReadKey` the application would terminate without waiting for the other methods to complete, or even start. You have to use `async Task Main()` and `await g();` to ensure the application works correctly – Panagiotis Kanavos Jan 20 '21 at 13:09
  • 1
    Does this answer your question? https://stackoverflow.com/questions/38017016/async-task-then-await-task-vs-task-then-return-task – rytisk Jan 20 '21 at 13:10
  • 3
    Alsways worth the read: [Eliding Async and Await](https://blog.stephencleary.com/2016/12/eliding-async-await.html) - StevenC's blog – Fildor Jan 20 '21 at 13:12
  • @PanagiotisKanavos I tested: in both cases removing `Readkey` has the same result. – Minimus Heximus Jan 20 '21 at 13:14
  • `async void` methods are intended for async event handlers of GUI applications (WinForms/WPF). In general should be avoided. You can learn more about `async void` methods [here](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void "Avoid async void"). – Theodor Zoulias Jan 20 '21 at 13:19

1 Answers1

0

Ok, Nice question. Let me explain you, In case you have used async void and the method throws some Exception as this process is done without awaiting the system will fail to handle the exception and the process terminates the application.

It is always advisable to use async Task or async Task<bool> or async Task<int> just await the method while calling to check every happened well or not.

It is bad practice to us async void