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