Dialects

Like most things in life, hardly anything actually ever goes to plan. I did intend on writing a more in-depth entry on the new async features in C# 5 during the weekend, but had an insane weekend filled with relatives visiting/being visited. It wasnt until monday afternoon that I actually had a chance to sit down and do a bit of tinkering in my VM and give this stuff a shot by myself.

And thats when the problems started.

You know, I find new technologies to be a lot like the dialects of a particular (spoken) language. Spanish, for instance, is spoken by many, many countries around the world, but each country with their own particular dialect. Each of these countries does has an understanding of what the other country is saying, but the things differ slightly, usually in grammar or simply completely different words for the same thing. But they usually get the gist of whats being said.

This is how I felt last thursday at the Perth .Net Usergroup. While I was able to follow what Joe Albahari was saying and understanding the concepts behind it, when it came to me actually trying to communicate with my computer in the async 'dialect', lets just say there was very bloody brick wall by the end of the night. And I had a huge headache.

For my very first foray into the world of asynchronous programming (as Ive never really tinkered with the .Net 2+ Threading stuff, not seriously) was a form with two controls, a text box and a button. And all that was (supposed) to happen was on the click of the Button, "A" would appear in the textbox, courtesy of an awaited method. Thats it. Seriously.

This was my code...

private async void btnStart_Click(object sender, RoutedEventArgs e)
{

Task<string> x = await TaskEx.Run(() => GetA());
txtResults.Text = x.Result;

}

private async Task<string> GetA()
{

return await new Task<string>(() => "A");

}

Those familiar (because I was not) with the Task classes introduced in .Net 4.0 will realise, as this kind gentleman pointed out for me, that I did not TaskEx.Run my new task. All I did was return it to the UI, and the x.Result locked out my UI thread (oh the irony!). Thank God for resources like MSDN and StackOverflow where MS employees will browse the forums and answer any number of completely inane questions. God (or whoever) bless you all!

When I changed my code in the GetA() method to:

return TaskEx.Run(() => "A");

It worked! Huzzah! I had the letter A printed in my fancy WPF text box.

But it just goes to show how the different dialects of technology can trip you up in the most simple ways. I think my problem is I came into this without knowing anything but the very basics of Tasks and how they work, otherwise I would have realised in order to utilize a Task you must run it. Haha, sometimes you just gotta laugh :)

In fact, looking at that code now, I just realised I can refactor it a bit (the first cut of code never counts! :P ).

private async void btnStart_Click(object sender, RoutedEventArgs e)
{

string result = await TaskEx.Run(() => "A");
txtResults.Text = result;

}

Easy as pie :)

Hopefully over the next few weeks, I want to think of slightly more complex (and maybe even real world) examples of where await and async might be used. Stay tuned!