Tag Archives: C# 2012 What’s New

C# 2012: Anatomy of an Async Method

There are two new keywords added to C# for asynchronous programming: async and await. The async keyword is a modifier for methods, delegates, and lambdas to indicate to the C# compiler that a method might contain async code. It also allows other code to call this method asynchronously. 

If a method doesn’t contain async code, it executes synchronously, but you will receive a warning message from the C# compiler. To run code asynchronously, use the await modifier when executing a method, anonymous method, or lambda. Here’s an example of using async and await keywords to implement an async method:

The async modifier indicates that StartAsync can contain async code. The await modifier calls the DoNetworkCommunicationsAsync method and waits for it to complete. In other words, it awaits the method.

Notice the suffix to the StartAsync method is Async. You aren’t required to add the Async suffix to the method name, but it’s a convention that Microsoft recommends and most developers follow in practice. Another tip on naming conventions refers to methods in earlier versions of .NET that implement EAP and already have the Async suffix—you would name them with the TaskAsync suffix.

To make the example simpler, the return type in the previous example is void, but async methods do have return types other than void. For now, let’s defer discussion of the return type until later, which is an important subject of its own.

The previous example doesn’t have any parameters by design in order to keep it simple. However, async methods can have parameters, but none of the parameters can be ref or out.

As in APM and EAP, as soon as the code calls an async method, with the await modifier, control returns to the caller while the method executes asynchronously. Expanding on the previous method, the following code demonstrates a basic example of how async methods work:

Let’s look at what this code shows in terms of control or what is running when. The specific threading model is defined separately for each .NET technology by a type derived from SynchronizationContext, but this is beyond the scope of this course.

The Main method calls the StartAsync method. Because StartAsync is modified as async, the code will run synchronously until it encounters a call to an async method with the await modifier. As soon as the code calls the method with await, control returns to the calling code Main. So you have two methods running at the same time—Main and StartAsync.

StartAsync continues by calling the DoNetworkCommunicationsAsync method. However, the await modifier makes StartAsync wait until DoNetworkCommunicationsAsync completes. When DoNetworkCommunicationsAsync completes, StartAsync ends. Control was already returned to Main when it encountered the await call to DoNetworkCommunicationsAsync, so StartAsync does not return at the end of the method, it just stops running.

That means that inside of StartAsync, all the code above the awaited method will run synchronously. At the same time, the async method runs. When
DoNetworkCommunicationsAsync completes, control returns to StartAsync and any code following StartAsync executes.

 

JoeMayoThis post is an excerpt from the online courseware for our C# 2012: Asynchronous Programming course written by expert Joe Mayo.

Joe Mayo is an author, independent consultant, and instructor specializing in Microsoft .NET and Windows 8 technology. He has several years of software development experience and has worked with .NET since July 2000.

C# 2012: Debugging Tips

For effective debugging, you need to be able to set a breakpoint. The easiest way to set a breakpoint is to click your mouse in the gutter, which is the vertical space to the left of the editor window, shown in the Figure below.

cswhatsnew

 

The Figure above. Click in the gutter to create a breakpoint.

After setting a breakpoint, you can start debugging by selecting DEBUG|Start Debugging or by pressing the F5 key. This will run the program until it reaches the breakpoint and then stop.

You can customize an individual breakpoint with conditions. Right-click on the red dot in the gutter and you’ll see options for Conditions, Hit Count, and more. You can use Hit Count to stop on the breakpoint after it executes a specified number of times, such as in a loop. Condition, shown in the Figure below, lets you define an expression that will make the code hit a breakpoint on that line when the expression is true.

CS What's New 2

 

The Figure above. You can set conditions on a breakpoint.

After setting a number of breakpoints, you’ll want to organize and keep track of them. You can open the Breakpoints window where you manage breakpoints, shown in the Figure above, by selecting DEBUG|Windows|Breakpoints.

CS What's New 3

 

The Figure above. The Breakpoints window lets you manage breakpoints. From the Breakpoints window, you can create new breakpoints, specify columns to view, and filter the list with Search. You can organize the breakpoints by right-clicking on a breakpoint and selecting Edit labels. Then you can search on labels and perform actions on the entire group of breakpoints as a whole.

When the program stops, you can inspect its state by hovering over variables with a mouse or viewing debugger windows.

 

JoeMayoThis post is an excerpt from the online courseware for our C# 2012: Language Changes and Updates course written by expert Joe Mayo.

Joe Mayo is an author, independent consultant, and instructor specializing in Microsoft .NET and Windows 8 technology. He has several years of software development experience and has worked with .NET since July 2000.