| By Hovhannes Avoyan | Article Rating: |
|
| April 23, 2012 09:00 AM EDT | Reads: |
2,231 |
In our last article we discussed guidelines for optimizing Threading. In this article we’ll look at best practices for using asynchronous calls. Asynchronous calls are non-blocking meaning when you call a method asynchronously, the calling thread returns immediately and continues execution of the current method.
Asynchronous Best Practices
This section summarizes best practices for optimized performance when using asynchronous execution:
Consider Client-Side Asynchronous Calls for UI Responsiveness
You can use asynchronous calls to increase the responsiveness of client applications. However, think about this carefully because asynchronous calls introduce additional programming complexity and require careful synchronization logic to be added to your graphical interface code.
The following code shows an asynchronous call followed by a loop that polls for the asynchronous call’s completion. You can add an exit criteria to the while condition in case you need to exit from function before call is completed. You can use the callback mechanism or wait for completion if you do not need to update the client.
IAsyncResult CallResult = SlowCall.BeginInvoke(slow,null,null);
while ( CallResult.IsCompleted == false)
{
… // provide user feedback
}
SlowCall.EndInvoke(CallResult);
Use Asynchronous Methods on the Server for I/O Bound Operations
You can increase the performance of your application by executing multiple operations at the same time. The two operations are not dependent on each other. For example, the following code calls two Web services. The duration of the code is the sum of both methods.
// get a reference to the proxy
EmployeeService employeeProxy = new EmployeeService();
// execute first and block until complete
employeeProxy.CalculateFederalTaxes(employee, null, null);
// execute second and block until complete
employeeProxy.CalculateStateTaxes(employee);
You can refactor the code as follows to reduce the total duration of the operation. In the following code, both methods execute simultaneously, which reduces the overall duration of the operation. Note that the following example uses the BeginCalculateFederalTaxes method, an asynchronous version of CalculateFederalTaxes; both of these methods are automatically generated when you reference a Web service from your client application in Visual Studio .NET.
// get a reference to the proxy
EmployeeService employeeProxy = new EmployeeService();
// start async call, BeginCalculateFederalTaxes
// call returns immediately allowing local execution to continue
IAsyncResult ar = employeeProxy.BeginCalculateFederalTaxes(employee, null, null);
// execute CalculateStateTaxes synchronously
employeeProxy.CalculateStateTaxes(employee);
// wait for the CalculateFederalTaxes call to finish
employeeProxy.EndCalculateFederalTaxes(ar);
Avoid Asynchronous Calls That Do Not Add Parallelism
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);
// Tasks 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. You should aim to avoid this practice.
In the next article of the .NET series, we’ll discuss Locking best practices and guidelines.
Read the original blog entry...
Published April 23, 2012 Reads 2,231
Copyright © 2012 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Hovhannes Avoyan
Hovhannes Avoyan is the CEO of Monitis, Inc., a provider of on-demand systems management and monitoring software to 50,000 users spanning small businesses and Fortune 500 companies.
Prior to Monitis, he served as General Manager and Director of Development at prominent web portal Lycos Europe, where he grew the Lycos Armenia group from 30 people to over 200, making it the company's largest development center. Prior to Lycos, Avoyan was VP of Technology at Brience, Inc. (based in San Francisco and acquired by Syniverse), which delivered mobile internet content solutions to companies like Cisco, Ingram Micro, Washington Mutual, Wyndham Hotels , T-Mobile , and CNN. Prior to that, he served as the founder and CEO of CEDIT ltd., which was acquired by Brience. A 24 year veteran of the software industry, he also runs Sourcio cjsc, an IT consulting company and startup incubator specializing in web 2.0 products and open-source technologies.
Hovhannes is a senior lecturer at the American Univeristy of Armenia and has been a visiting lecturer at San Francisco State University. He is a graduate of Bertelsmann University.
- Cloud People: A Who's Who of Cloud Computing
- Google Compute enters the IaaS market
- Cloud Expo NY: Environmental Pressures Drive an Evolution in File Storage
- The Software Freedom Conservancy – Fundraising Campaign: Non-Profit Accounting Software
- Cloud Expo NY: Interconnected Machines and the Future of Energy
- Cloud Conversations: AWS EBS, Glacier and S3 Overview | Part 3
- Healthcare Data on the Cloud – The Reality of Sensitive Information Online
- Cloud Business Solutions, Social Media, and Platform Systems of Engagement Market Shares, Strategies, and Forecasts, Worldwide, 2013 to 2019
- Google Submits Concessions to EC; Gets Sued in the UK
- Step-by-Step: Extend Your Network to the Cloud with Windows Azure Virtual Networks
- Cloud Expo New York | Storage & Archive: Are Existing Offerings Relevant?
- Shadow IT – The Reality Is Here
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: How to Use Google Apps Script
- Apple Ordered to Pay VirnetX $333K a Day
- Google Compute enters the IaaS market
- Cloud Expo NY: Environmental Pressures Drive an Evolution in File Storage
- The Software Freedom Conservancy – Fundraising Campaign: Non-Profit Accounting Software
- Cloud Expo NY: Interconnected Machines and the Future of Energy
- Cavalry Rides into Oracle’s Java Suit
- Samsung Uses Centrify for Safer Android Platform
- Cloud Conversations: AWS EBS, Glacier and S3 Overview | Part 3
- Google Maps May Be Banned in Germany
- Healthcare Data on the Cloud – The Reality of Sensitive Information Online
- Where Are RIA Technologies Headed in 2008?
- Personal Branding Checklist
- The Top 250 Players in the Cloud Computing Ecosystem
- AJAXWorld 2006 West Power Panel with Google's Adam Bosworth
- Why Microsoft Loves Google's Android
- Google's OpenSocial: A Technical Overview and Critique
- Cloud People: A Who's Who of Cloud Computing
- Wal-Mart To Sell $399 Ubuntu Linux-based Laptop with Google Operating System
- Cloud Expo New York Call for Papers Now Open
- Dolphin Announces Open API With Over 50 Add-ons Including Dropbox and Wikipedia
- i-Technology Blog: Google Trends on Java, McNealy, AJAX, and SOA Give Pause For Thought
- i-Technology Blog: Is There Life Beyond Google?























