twitter rodrigue_ linkedin rodrigue hajjar

PDC10: After effect (asynchronously)

Tags: pdc10, C#

Hope the title isn’t too confusing and the word play was understood ;).. if not maybe will be after reading the post.

So here I am back from the PDC10, back at work and started my masters.

My pdc10 badge is still in my laptop bag (I don’t wannaaa take it off!!! )

So reflecting on all this, I’ve been asked…if there was another developer challenge, would you of participated in it knowing all the fatigue there was from it? I’d definitely say yes again and again...

Plus what I realized at the pdc10 is: every time I talked about Deed Day, I got really positive feedback. So that’s encouraging me even more to make it happen!

You can check out an interview of me on Channel9 by Lara Foy where I demo Deed Day rapidly and talk about it: http://channel9.msdn.com/Blogs/LauraFoy/PDC-10-Student-Apps-for-Windows-Phone-7 (I’m at around 7:40)

Oh and this post is going to be a bit technical, one of the many things revealed at the PDC, this time from the Linq to Everywhere sessions; “await” and ”async” keywords (which are my 2 favorite keywords now ;)  and are probably going to be implemented in v5.0 of C#)..

You can get the CTP from here.

So after you’ve installed it and rebooted.

You can launch test projects and demos from the intro page found here: C:\Users\**** \Documents\Microsoft Visual Studio Async CTP\Documentation

Now after looking around, I thought I could show you some interesting scenarios:

Let’s say you have 2 webcalls to make, but they have to be in a specific order, what do you usually do? Probably use Actions and after a callback of the first call do the second one (or if the call completed event is implemented call the second service from there)

With the await keyword, you’d end up with a lot less code!

var client = new WebClient();
Console.WriteLine(await client.DownloadStringTaskAsync(new Uri("http://www.channel9.msdn.com")));
Console.WriteLine(await client.DownloadStringTaskAsync(new Uri("http://www.microsoft.com")));

So what happens here? Thanks to the “await” keyword, Console.WriteLine() won’t be executed until the call is complete.

Another use might be in case you want to really transform a sync method to an async method with Task<T> and await, you can later find out whether the Task is completed or not etc..:

 static async Task<string> TaskAsyncCall()
 {
     return await new WebClient().DownloadStringTaskAsync(new Uri("http://channel9.msdn.com"));
 }

After that using calling the method (I’m on a console for the demo):

Task<string> task = TaskAsyncCall();
 while (!task.IsCompleted)  {          Console.WriteLine("working");  }  Console.WriteLine(task.Result);  Console.ReadLine();

You’ll notice after the “TaskAsyncCall” the Console will keep writing “working” until the call is completed (so no UI thread is blocked).

So start playing with “Visual Studio Async CTP” now ;)

One other thing I’m excited about and for the first time learned about in the pdc10 is the “Rx extensions”, but that will come in the next blog post!

 

Add a Comment