Telerik blogs

The latest internal build of JustCode has been released with support for the asynchronous language features in the Microsoft Visual Studio Async CTP. Improvements to the semantic analyzer detect errors on usage of the async method modifier and await operators in C# and Visual Basic. This feature will be available for the general public in JustCode Q2 SP1.

If you’re new to the async and await keywords and would like to know to learn asynchronous programming with C# 5, refer to Going Asynchronous. I provide an example of a twitter client using synchronous programming, the current way of doing asynchronous programming, and how to refactor it to use the language features in the CTP.

Using the new languages constructs requires the project to include a reference to AsyncCtpLibrary.dll. This is typically located in %HOMEPATH%\Documents\Microsoft Visual Studio Async CTP\Samples.

Methods marked async must return void, Task, or Task<T>. No return statement is used for async methods with Task as the method return type. JustCode displays an error for this.

AsyncError04

This is valid without the return condition, and it is equivalent to an async void method.

 

public async void GetData()
{
    WebClient client = new WebClient();
    this.Data = await client.DownloadStringTaskAsync(GetUri());
}

 

If a return value is needed, then use the generic Task<T> class. The return statement should return an object of type T. For example, the method’s return type is Task<string>, an async method should return a string.

 

public async Task<string> GetData()
{
    WebClient client = new WebClient();
    string result = await client.DownloadStringTaskAsync(GetUri());
    return result;
}

Since the return type is Task<string> and result is a string, JustCode correctly identifies this as valid. However, if the return type is not the type of T in Task<T>, the statement is marked as an error.

 
AsyncError01
 

If you used the await keyword but forgot to mark the message async, an error is show and a quick fix is suggested.

 

AsyncError03

 

AsyncErrorQuickFix

 

It is invalid to await from within a catch or finally block, and JustCode will alert you to these issues as well.

AsyncError02

 

A warning is shown if a method marked async does not contain an await.

AsyncWarning1

 

The new keywords, async and await, make asynchronous programming much more elegant than in the past. If you choose to take advantage of it, you can trust JustCode to continue to support your productivity needs.


About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Comments

Comments are disabled in preview mode.