I'm wandering whether .Net
async method run in new threads or in the main one.
For example, In my Console Application, I invoke an
async method to download a URL content using
Will this method (GetStringAsync) run in new separate thread?
In general, asynchronous I/O methods do not use separate threads. I explain this in detail in my blog post There Is No Thread.
However, in this specific case, that's not quite true.
WebRequest-based APIs in .NET have supported asynchronous operations for a long time, but have actually always done HTTP proxy detection and DNS lookup as synchronous, even through their asynchronous APIs. This was noticed when
HttpClient started becoming popular. Unfortunately, Microsoft decided not to fix these long-standing bugs.
HttpClient wraps its
WebRequest calls in a thread pool thread. Note that it is still using the "asynchronous" APIs, so only the synchronous portion (HTTP proxy and DNS lookup) are done on a thread pool thread; the rest of the request is truly asynchronous. At least, this is true for some platforms.