I have read a dozen articles about async/await and they all have been a great read. But in all those articles some confusions/questions weren't addressed. I have seen on this forum too (this awesome post) but I haven't got clear explanation,so I am seeking your profound knowledge on the matter.
Example
In the below example of Console App as synchronization context is not maintained the thread ID's are returned namely 1 and 5.
(A similar example with GUI would run on the same thread as synchronization context is maintained there.)
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncConcept
{
class Program
{
static void Main(string[] args)
{
AsyncMethod();
Console.WriteLine("Main Thread: " + (int)AppDomain.GetCurrentThreadId());
Console.ReadLine();
}
public static async void AsyncMethod()
{
await Task.Delay(TimeSpan.FromSeconds(5));
Console.WriteLine("AsyncMethod Thread: " + (int)AppDomain.GetCurrentThreadId());
Console.ReadLine();
}
}
}
Questions
When we use await keyword with let's say an I/O call or an API call, does await operation happens on Main thread only?
a) If yes, the how come the Main thread (UI thread in case of GUI) continue with remainder of the method simultaneously ?
b) If No, then it violates the No thread spawn theory of async and await.