Avoid Asynchronous Calls That Do Not Add Parallelism

2022-03-13

Avoid asynchronous calls that will block multiple threads for the same operation. The following code shows an asynchronous call to a Web service. The calling code blocks while waiting for the Web service call to complete. Notice that the calling code performs no additional work while the asynchronous call is executing.

// get a proxy to the Web service
  customerService serviceProxy = new customerService (); 
  //start async call to CustomerUpdate 
  IAsyncResult result = serviceProxy.BeginCustomerUpdate(null,null); 
  
// Useful work that can be done in parallel should appear here
// but is absent here
//wait for the asynchronous operation to complete
// Client is blocked until call is done
  result.AsyncWaitHandle.WaitOne(); 
  serviceProxy.EndCustomerUpdate(result); 
  
When code like this is executed in a server application such as an ASP.NET application or Web service, it uses two threads to do one task and offers no benefit; in fact, it delays other requests being processed. This practice should be avoided.
Ref: https://msdn.microsoft.com/en-us/library/ff647790.aspx

0 comments: