I read many articles said that async/await doesn't create additional threads. But the message from Output and Thread windows in debug mode of Visual Studio said the contrary.
I created a very simple example windows form with some code;
private void button2_Click(object sender, EventArgs e)
{
Task t = methodAsync();
//t.Wait();
}
async Task methodAsync()
{
Console.WriteLine($"==before DownloadStringTaskAsync");
using (var wc = new System.Net.WebClient())
{
string content = await wc.DownloadStringTaskAsync("https://stackoverflow.com");
}
Console.WriteLine($"==after DownloadStringTaskAsync");
}
I start app in debuging mode, I pause it by clicking pause button on Debug toolbar. Threads windows show there is only one Main thread, that's normal so far.
Then I click on button to execute methodAsync
. When it complete DownloadString, I pause app again, and then I see serveral additional thread in Thread windows.
After about 10 seconds the Output windows shows message "The thread xxx has exited with code 0 (0x0)"
.
The same result when I replace WebClient.DownloadStringTaskAsync
with await Task.Delay(xxx)
I wonder if async/await does really create new thread or not.
Any explaination?