However, if you take a look at the code for that method, you will notice there is no code to create any Task and that the return statement returns the result of a sum of two decimal variables. The code just returns a decimal but the async modifier will wrap and unwrap the decimal variable under the hoods. We don't need to think about either tasks or continuations, the code is pretty similar than synchronous code. The method represent a very common case in which we need to wait for I/O bound operations to compute a final result and return it. The method performs the following steps:
- Call the
GetSubTotalAsyncmethod with an asynchronous execution to compute the subtotal. - Call the
GetOtherChargesAsyncmethod with an asynchronous execution to compute the other charges. - Compute the sum of the subtotal plus the other charges.
- Calculate the sales tax amount based on the subtotal plus other charges computed in the aforementioned step.
- Return the sum of the subtotal plus other charges plus the sales tax amount.
The code that retrieves the subtotal uses the await keyword make the call to the GetSubTotalAsync method with an asynchronous execution.
var subTotal = await GetSubTotalAsync();
The next line won't be executed until the GetSubTotalAsync method finishes its execution and the subTotal variable has the value returned from the method or an exception occurs. Thus, the code behaves just like synchronous code. I will dive deeper on exception handling later.
You can think of the GetSubTotalAsync method as the typical I/O bound operation that you don't want to block the UI until it finishes. In this case, I used the new DownloadStringTaskAsync method for the WebClient class. .NET Framework 4.5 added many methods with the Async suffix that can be called with the await modifier to the WebClient class and to other .NET classes. The code just downloads the contents of Dr Dobb's home page with an asynchronous execution and just converts the first character to a decimal value. Forgive me for the simplicity; I want to keep the example as simple as possible while providing some real-life I/O bound operations.
The GetOtherChargesAsync method does the same thing than the GetSubTotalAsync method but it takes the second character of the string instead of the first one. Again, the idea was to have two real I/O bound operations.
The following listing shows the code for MainWindow.xaml. The simple UI displays a button and a listbox on a WPF Window (see Figure 1).
<Window x:Class="WpfAsyncAwaitSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="UpdateTotal" Content="Add total" HorizontalAlignment="Left" Height="66"
Margin="10,10,0,0" VerticalAlignment="Top" Width="252" Click="UpdateTotalClick"/>
<ListBox Name="TotalList" HorizontalAlignment="Left" Height="220" Margin="10,91,0,0"
VerticalAlignment="Top" Width="498" />
</Grid>
</Window>

Figure 1. The UI design with a button and a listbox. The button calls an asynchronous method and doesn't block the UI.
The following listing shows the code for MainWindow.xaml.cs, the interaction logic for MainWindow. It's very simple the UpdateTotalClick even handler calls MathService.GetCalculatedTotalAsync with an asynchronous execution and then adds the calculated total to the TotalList listbox. Notice that the event handler method declaration includes the async modifier because it uses the await keyword to execute MathService.GetCalculatedTotalAsync. To keep things simple, I didn't create a MVVM (short for Model-View-ViewModel) WPF solution.
namespace WpfAsyncAwaitSample
{
using System.Windows;
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private async void UpdateTotalClick(object sender, RoutedEventArgs e)
{
//// Sales Tax = 8.5% = 0.0850
const decimal salesTax = 0.0850M;
var total = await MathService.GetCalculatedTotalAsync(salesTax);
TotalList.Items.Add(total);
}
}
}



