• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] async and await
#1
let's begin with a some general things about async and await keywords
MSDN Wrote:The Async and Await keywords in Visual Basic and the async and await keywords in C# are the heart of async programming. By using those two keywords, you can use resources in the .NET Framework or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method. Asynchronous methods that you define by using async and await are referred to as async methods.

Let's start now. First up create a new WPF project targeting .net 4.5 in visual studio 2012 and name is Asynchronus Programming
[Image: 8fce7d1641cf55ddec0e610aeb5f7f60.png?1363093835]
Now let's add some controls to MainWindow. I added a button named start and a progress bar named pb. Create event handler for button click this can be done easily by double clicking and set the isIndertiminate property of progress bar to false. The resulting XAML is
Code:
<Window x:Class="Asynchronus_Programming.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="180" Width="223">
    <Grid Margin="0,0,2,4">
        <Button x:Name="Start" Content="Begin" HorizontalAlignment="Left" Margin="130,105,0,0" VerticalAlignment="Top" Width="75" Height="25" Click="Start_Click"/>
        <ProgressBar x:Name="pb" HorizontalAlignment="Left" Height="23" Margin="10,59,0,0" VerticalAlignment="Top" Width="195" IsIndeterminate="False"/>

    </Grid>
</Window>
[Image: 4159555766a07989d57e6be44c7e5f0e.png?1363093998]

Now let's write some code. First up notice the Sytem.Threading.Tasks namespace , this is the backbone of this tutorial.
[Image: 7a65b69bfef63bd17d73a84e3c6013b3.png?1363094282]

Next modify the button click method to have the keyword async, this is necessary
[Image: d455a5ce0731b617d2772aa5d982d146.png]

Add a reference to System.Net namespace.
[Image: 75182c13a8b2eea985aac537565a6d3f.png?1363094507]

Write an async function called DownloadGoogleAsync with return type of Task<string> . This method returns a string . Add the following code(You can replace url with any website you like but it's just easy to use google)
CSHARP Code
  1. WebClient webClient = new WebClient();
  2. Task<string> Result = webClient.DownloadStringTaskAsync("http://google.com");
  3. string result=await Result;
  4. Dispatcher.Invoke(() => pb.IsIndeterminate = false);
  5. return result;

Now it should look like
[Image: 6032fa4cba5ddd6aaa12483b7aec5e35.png?1363095269]

Finally add the following code in the button click event
CSHARP Code
  1. Task<string> Result = DownloadGoogleAsync();
  2. pb.IsIndeterminate = true;
  3. MessageBox.Show(await Result);

[Image: 86aefb736465ff8759e3735836c25453.png?1363095141]

Now compile it and see the magic of async programming. Hoping that this helped you also please let me know about any mistakes.
[Image: 25f11fee62009bc227f0d143820c96ed.png?1363095337]
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)