I’ve heard a lot about how a new thread is not created with async awiat. I decided to check what would happen if there was an while(true) in the main function and the asynchronous function.
namespace asyncTest
{
public class Program
{
public static void Task1()
{
while (true) {
Console.WriteLine("Task1: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
}
}
public static async void async()
{
Console.WriteLine("async start: " + Thread.CurrentThread.ManagedThreadId);
await Task.Run(Task1);
Console.WriteLine("async end: " + Thread.CurrentThread.ManagedThreadId);
}
static void Main(string[] args)
{
async();
while (true) {
Console.WriteLine("main: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
}
}
}
}
But for some reason my functions are executed in different threads:
I want to understand why they are executed in different threads if async await does not create a thread. And I want to know how high-load operations are performed asynchronously, as in this example. Who performs the asynchronous operation on an additional thread? or are they executed in the same thread?