The importance of context, and a question of explicitness

(Just to be clear: I hate the word "explicitness". It reminds me of Rowan Atkinson as Marcus Browning MP, saying we should be purposelessnessless. But I can’t think of anything better here.) For the last few days, I’ve been thinking about context – in the context of C# 5’s proposed asynchronous functionality. Now many of the examples which have been presented have been around user interfaces, with the natural restriction of doing all UI operations on the UI thread. While that’s all very well, I’m more interested in server-side programming, which has a slightly different set of restrictions and emphases. … Continue reading The importance of context, and a question of explicitness

Multiple exceptions yet again… this time with a resolution

I’ve had a wonderful day with Mads Torgersen, and amongst other things, we discussed multiple exceptions and the way that the default awaiter for Task<T> handles an AggregateException by taking the first exception and discarding the rest. I now have a much clearer understanding of why this is the case, and also a workaround for the cases where you really want to avoid that truncation. Why truncate in the first place? (I’ll use the term "truncate" throughout this post to mean "when an AggregatedException with at least one nested exception is caught by EndAwait, throw the first nested exception instead". … Continue reading Multiple exceptions yet again… this time with a resolution

Using extension method resolution rules to decorate awaiters

This post is a mixture of three things: Revision of how extension methods are resolved Application of this to task awaiting in async methods A rant about void not being a type Compared with my last few posts, there’s almost nothing to do with genuine asynchronous behaviour here. It’s to do with how the language supports asynchronous behaviour, and how we can hijack that support 🙂 Extension methods redux I’m sure almost all of you could recite the C# 4 spec section 7.6.5.2 off by heart, but for the few readers who can’t (Newton Microkitchen Breakfast Club, I’m looking at … Continue reading Using extension method resolution rules to decorate awaiters

Propagating multiple async exceptions (or not)

In an earlier post, I mentioned  that in the CTP, an asynchronous method will throw away anything other than the first exception in an AggregateException thrown by one of the tasks it’s waiting for. Reading the TAP documentation, it seems this is partly expected behaviour and partly not. TAP claims (in a section about how "await" is achieved by the compiler): It is possible for a Task to fault due to multiple exceptions, in which case only one of these exceptions will be propagated; however, the Task’s Exception property will return an AggregateException containing all of the errors. Unfortunately, that … Continue reading Propagating multiple async exceptions (or not)

Configuring waiting

One of the great things about working at Google is that almost all of my colleagues are smarter than me. True, they don’t generally know as much about C#, but they know about language design, and they sure as heck know about distributed/parallel/async computing. One of the great things about having occasional contact with the C# team is that when Mads Torgersen visits London later in the week, I can introduce him to these smart colleagues. So, I’ve been spreading the word about C# 5’s async support and generally encouraging folks to think about the current proposal so they can … Continue reading Configuring waiting

Evil code – overload resolution workaround

Another quick break from asynchrony, because I can’t resist blogging about this thoroughly evil idea which came to me on the train. Your task: to write three static methods such that this C# 4 code: static void Main() {     Foo<int>();     Foo<string>();     Foo<int?>(); } resolves one call to each of them – and will act appropriately for any non-nullable value type, reference type, and nullable value type respectively. You’re not allowed to change anything in the Main method above, and they have to just be methods – no tricks using delegate-type fields, for example. (I don’t know whether such … Continue reading Evil code – overload resolution workaround

Dreaming of multiple tasks again… with occasional exceptions

Yesterday I wrote about waiting for multiple tasks to complete. We had three asynchronous tasks running in parallel, fetching a user’s settings, reputation and recent activity. Broadly speaking, there were two approaches. First we could use TaskEx.WhenAll (which will almost certainly be folded into the Task class for release): var settingsTask = service.GetUserSettingsAsync(userId);  var reputationTask = service.GetReputationAsync(userId);  var activityTask = service.GetRecentActivityAsync(userId);  await TaskEx.WhenAll(settingsTask, reputationTask, activityTask);  UserSettings settings = settingsTask.Result;  int reputation = reputationTask.Result;  RecentActivity activity = activityTask.Result; Second we could just wait for each result in turn: var settingsTask = service.GetUserSettingsAsync(userId);   var reputationTask = service.GetReputationAsync(userId);   var activityTask = service.GetRecentActivityAsync(userId);          … Continue reading Dreaming of multiple tasks again… with occasional exceptions

C# in Depth 2nd edition: ebook available, but soon to be updated

Just a quick interrupt while I know many of you are awaiting more asynchronous fun… Over the weekend, the ebook of C# in Depth 2nd edition went out – and a mistake was soon spotted. Figure 2.2 was accidentally replaced by figure 13.1. I’ve included it in the book’s errata but we’re hoping to issue another version of the ebook shortly. Fortunately this issue only affects the ebook version – the files shipped to the printer are correct. Speaking of which, I believe the book should come off the printing press some time this week, so it really won’t be … Continue reading C# in Depth 2nd edition: ebook available, but soon to be updated

Control flow redux: exceptions in asynchronous code

Warning: as ever, this is only the result of reading the spec and experimentation. I may well have misinterpreted everything. Eric Lippert has said that he’ll blog about exceptions soon, but I wanted to put down my thoughts first, partly to see the difference between what I’ve worked out and what the real story is. So far, I’ve only covered "success" cases – where tasks complete without being cancelled or throwing exceptions. I’m leaving cancellation for another time, but let’s look at what happens when exceptions are thrown by async methods. What happens when an async method throws an exception? … Continue reading Control flow redux: exceptions in asynchronous code