I'm wondering 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?Answer1:
In general, asynchronous I/O methods do not use separate threads. I explain this in detail in my blog post <a href="http://blog.stephencleary.com/2013/11/there-is-no-thread.html" rel="nofollow">There Is No Thread</a>.
However, <em>in this specific case</em>, 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 <em>synchronous</em>, even through their asynchronous APIs. This was noticed when
HttpClient started becoming popular. Unfortunately, Microsoft decided <em>not</em> 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 <em>some</em> platforms.
No. Because using async / await is mainly created to accomplish IO-bound tasks asynchronously not creating new threads. Only with CPU bound tasks new threads are created.