Async CTP (SP1 Refresh)

Async CTP (SP1 Refresh)

Rate This
  • Comments 15

By Jeremy Meng

Visual Studio Async CTP (SP1 Refresh) is available now! You are welcome to download and try it out!

Thanks to the new Async feature coming in Visual Basic and C# it has never been so easy to write asynchronous code. With the newly introduced async and await keywords you can make asynchronous calls without having to create callback functions, sign them up to some ****Completed events, retrieve results from EventArgs by yourself. Visual Basic and C# compiler will do the plumbing work for you.

I hope that the following example can help you get basic ideas on how the Async feature makes writing responsive applications easier.

Here is a simple WPF application to get you started with Async in C#. This WPF app has a label, a button, and a checkbox on its UI. When the button is clicked, the application will perform a query over the network (for example, downloading files from the internet, querying data from a database, etc.) After the query is done, the label will be updated to show the length of the returned string data.

The event handler for button click:

private void button1_Click(object sender, RoutedEventArgs e)

{

    UpdateLabel();

}


async void UpdateLabel()

{

    NetworkClass nc = new NetworkClass();

    int i = 0;

    i = await nc.MethodQueryingNetworkAsync();

    label1.Content = "Length: " + i;

}

 

The code for MethodQueryingNetworkAsync() is like below:

async public Task<int> MethodQueryingNetworkAsync()

{

    // Querying a slow website with network latency so that

    // the result comes back after a certain amount of time

    WebClient wc = new WebClient();

    string content = await wc.DownloadStringTaskAsync(
       
new Uri("http://some.slow.website/"));

    return content.Length;

}

 

Run the application and click on the button, you will still be able to toggle the checkbox on UI while the network query is executing. When the downloading is finished, the label will be updated to show the length of the query result. During the query execution, the UI is completely responsive.

What’s new in Visual Studio Async CTP SP1 Refresh?

  • It is compatible with Visual Studio 2010 SP1 and Windows Phone 7. The previous Async CTP only works for Visual Studio 2010 RTM.
  • More efficient Async methods!

We changed the old pattern of GetAwaiter/BeginAwait/EndAwait with the new pattern of GetAwaiter/IsCompleted/OnCompleted/GetResult. This provides better performance especially if the task we are awaiting had already completed. We also make exception behaviors uniform in void-returning async methods (Async Subs in Visual Basic).

  • Many bug fixes since the release of the previous Async CTP. Thanks for the feedbacks from the community! Please let us know what you think!

For more details, read on Lucian’s blog. Welcome to Async and happy asynchronous coding!

Leave a Comment
  • Please add 7 and 5 and type the answer here:
  • Post
  • I started the Async-CTP.exe normally (no manual elevation) in win7 64.

    It's now at NDP40-KB0000002 for last few minutes, didn't ask elevation during execution. Cancel button is not available. I suspect that NDP40-KB0000002 can't be installed as without admin priv.

    I'll leave it running but it's not looking good may have to kill it.

  • Actually now that I try running the exe again it does elevate - maybe I just clicked OK so fast on the first time I forgot about doing it.

    Update: after ~25 minutes, it no longer says NDP40-KB0000002. It's now at VS10-KB0000001. Slowest non-OS install I've seen ever that's 20 MB in size.

  • Update: Now it finished "OK" after some 26 minutes. Does the installer put the install log somewhere and do you need it for debugging why was this so slow? I'll try to check back this comment section later.

  • The install was "success" but none of the examples compile and some of the projects don't even open. I have 2010 Ult SP1. I didn't check the "send install log automatically" option.

    I archived these logs from the temp directory that were made during the install. http://bit.ly/eMheAt

    I notice the NGEN may have taken long time during the install, there should be better progress indicator if the NDP40-KB0000002 is going to always take 30 minutes.

  • Success: I got it succesfully installed and 101 samples compiled. This was after went to Installed Updates and uninstalled both the broken Async ctp SP1 refresh and the earlier Async CTP I thought I had uninstalled before.

    I still get

    error  : The project file '\Microsoft Visual Studio Async CTP\Samples\(C# WCF) Stock Quotes\WCFQuoteCalculatorService\WCFQuoteCalculatorService.csproj' cannot be opened.

    I do have WCF obviously but this thing requires something else ? I haven't got SQL/web stuff installed.

    The project type is not supported by this installation.

    Also the startup page for async ctp has double-edged sword. When clicking the C#/VB project, if VS2010 is cold-started it will seem like nothing is happening for good 10-20 seconds while it's loading up the first time, so user is tempted to press it again quick since it's not clear that the loading process was actually started.

  • 10 days later and no feedback on the above install?

  • Dude...what's wrong with you?  Why are you giving everyone a play by play update on your installation. Is it really worth spamming a comment board with messages that no one cares about to say that it took 26 minutes to install the CTP?  What if people had real questions or concerns that weren't noticed because of your constant updates?  I really don't think the async pattern is for you.  You seem like a single threaded apartment kind of guy.

  • @zzz @yyy .... NO comments, because, people, this site's comments are meant to comment on the article, not the install of the CTP.   Use connect or some other avenue to enter comments on the CTP, not article comments.

    Async is a great idea, guys.  Thanks for the update.

    -e

  • Way Cool  - Good Info Chef - but to try it - it has to be installed.

    Yeah - play by play is perhaps neurotic - but apparently nobody else stopped by at all until now.

  • Seems similar to—but less cool than—Reactive Extensions (Rx).  Built in keywords are kind of cool, but I dislike having to permanently change methods to be async... usually that choice is left up to the caller rather than the callee.

  • "I really don't think the async pattern is for you.  You seem like a single threaded apartment kind of guy."

    comment of the day.

    In other news, great little article. Shown at such a simple level I could take one look and "get" it. job done, off to overhaul some GUI's with this now!

  • zzz : same problem as you. Setup blocked during NDP40-KB0000002 installation.

    someone else got the same problem ?

  • It took one hour for fully installation. Async CTP works for now.

    To check the installation : blogs.msdn.com/.../async-ctp-refresh-installation.aspx

  • Resharper and other third-party extensions doesn't support Async keywords and you still get squiggly underlines within Visual Studio and errors within the editor.

    Don't worry, the code will compile anyway. The new keywords are recognized by the compiler. By the way, you will have no error in Visual Studio.

  • The problem is, sometimes we want to create the method

    as synchronusly instead of async

    so with the new keyword, we still have to write 2 methods each ?

Page 1 of 1 (15 items)