From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perfo
Showing the above explanations in action in a simple console program:
class Program
{
static void Main(string[] args)
{
TestAsyncAwaitMethods();
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
public async static void TestAsyncAwaitMethods()
{
await LongRunningMethod();
}
public static async Task LongRunningMethod()
{
Console.WriteLine("Starting Long Running method...");
await Task.Delay(5000);
Console.WriteLine("End Long Running method...");
return 1;
}
}
And the output is:
Starting Long Running method...
Press any key to exit...
End Long Running method...
Thus,
TestAsyncAwaitMethods
. That immediately returns without halting the current thread and we immediately see 'Press any key to exit' messageLongRunningMethod
is running in the background. Once its completed, another thread from Threadpool picks up this context and displays the final messageThus, not thread is blocked.