They say Task.Delay() is an async Thread.Sleep(). To test this I wrote below code. I expect to print immediately "One" then 3 seconds later result variable (15) will be printed. 2 seconds after this, "Two" will be printed. But it doesnt seem so. "One" is not immediately printed. "One" is printed 3 seconds later. Why does it wait 3 seconds to print "One" ?
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication31
{
class Program
{
public static int Multiply(int a, int b)
{
return a * b;
}
public static async Task DoRunAsync(int m, int n)
{
int result = await Task.Run(() => Multiply(m, n));
await Task.Delay(3000);
Console.WriteLine("One");
Console.WriteLine(result);
}
static void Main(string[] args)
{
Task t = DoRunAsync(3, 5);
Thread.Sleep(5000);
Console.WriteLine("Two");
}
}
}