2035

Will HttpClient Async methods run in new threads

Question:

I'm wondering whether .Net HttpClient 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 HttpClient.GetStringAsync method.

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.

So, 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.

Answer2:

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.

Recommend

  • cordova application “Select” dropdown not working in iOS9
  • Changing a graph to list of nodes in Haskell
  • Arrow is not drawn correctly on fabricJS canvas
  • Post hoc tests with ezANOVA output
  • How to extend Sequelize model
  • Can an Entity access a Repository?
  • Exception in thread “main” java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibi
  • Insertion Sort in C#
  • Eloquent Javascript: Can't understand how the number value is determined in the sum function
  • How to install or uninstall SonarQube plug-ins with HTTP?
  • Downgrade NG2 Directive to AngularJS
  • Weekday as String to number
  • CameraPreviewImageSource empty preview frame
  • UIImage to UIColor array of pixel colors
  • CABasicAnimation creates empty default value copy of CALayer
  • ggplot2 facet_grid with distinct x-axis labels using facet_grid
  • Complex multiple if statements
  • watir webdriver - window not found
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • Adding Dynamic Row and Data on Checkbox Click
  • How to organize this layout with overflows?
  • Scripting Support For Image Filtering in DigitalMicrograph
  • Adding native code to an existing Worklight hybrid app
  • Issue with Terrain Collision using Three.js
  • How to output data of primefaces DataGrid component by columns instead of by rows (transpose data)?
  • How to control xtics in gnuplot
  • Visual Studio 2017 Professional- Unable to find package at source
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Regex not working in java 1.5
  • how to read to huge file into buffer
  • How to write seo friendly url's using htaccess?
  • Floating parent div grows to hypothetical width of floating child div
  • Conflicting declaration using constexpr and auto in C++11
  • How to specify generic type when the type is only known at runtime?
  • Stacked bar chart with continuous time-axis as x-axis
  • Another “Cannot make static reference…” Question
  • Did not understand process of initialize in swift programming
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Call Microservice from another Microservice within Docker
  • Time Complexity of Fibonacci Algorithm [duplicate]