Anders Hejlsberg – Pure Genius
If you have no idea who Anders is (for shame!) then like any true computer geek you probably turn to Wikipedia. Yes, you know you’ve made it when you get your very own wiki page. Seriously though, Anders is a Technical Fellow in the Developer Division here at Microsoft and I had the pleasure to attend a few sessions of his and to say that he understands computing languages and the nuisances they hold would clearly be an understatement.
Once such session was a talk he recently gave on “The future of C# and VB” where he laid out his vision of where both languages would be headed and one of those concepts (the Async framework) is coming to fruition. He demonstrated this framework which lead to an obvious Q&A dialog we had around the new async/await keywords.
Question: Since no new thread is being instantiated (e.g. background thread) and all the processing is done on the UI thread thereby eliminating thread concurrency issues, how is the UI thread not blocked after the firing of an asynchronous event?
Answer: The compiler rewrites the code with regular flow of control into continuation-passing style code. For example, this code:
async void ProcessFeedAsync(string url) {
var text = await DownloadFeedAsync(url);
vardoc = ParseFeedIntoDoc(text);
await SaveDocAsync(doc);
ProcessLog.WriteEntry(url);
}
is rewritten into something similar to this
void ProcessFeedAsync(string url) {
DownloadFeedAsync(url).ContinueWith(t1 => {
var text = t1.GetResult();
var doc = ParseFeedIntoDoc(text);
SaveDocAsync(doc).ContinueWith(t2 => {
ProcessLog.WriteEntry(url);
});
});
}
The imaginary DownloadFeedAsync method starts an asynchronous download from the given URL and immediately returns a Task object that represents the ongoing asynchronous operation. The code then registers a continuation (a “callback”) which the task will invoke once it completes. Following the callback registration there is nothing further to do, so the outer ProcessFeedAsync method simply returns to its caller. That in turn allows other work to take place on the calling thread (which might be the UI thread). Once the asynchronous download completes, it invokes the callback on the same thread that it was started on (in this case by posting a message to the message loop) and the remainder of the ProcessFeedAsync method runs simply like any other work on the UI thread. But in between initiation and completion of the download other messages can be handled on the UI thread—at no point is the UI thread blocked.
I mention this conversation as an example of how Anders can convey an extremely complex subject in terms that a much broader audience can understand. Those who have the ability to do this “sharing of knowledge” are truly gifted. It’s definitely an art as not too many Physicists can explain string theory to the public at large.
Summary
If you ever have a chance to attend an event that Anders will be speaking at, run don't walk (or click as the case may be) and register for it as you won’t be disappointed. In the interim, Channel9 has a fantastic video of Anders answering a whole host of questions and is certainly worth the time investment.