Eduasync part 11: More sophisticated (but lossy) exception handling

(This post covers projects 13-15 in the source code.) Long-time readers of this blog may not learn much from this post – it’s mostly going over what I’ve covered before. Still, it’s new to Eduasync. Why isn’t my exception being caught properly? Exceptions are inherently problematic in C# 5. There are two conflicting aspects: The point of the async feature in C# 5 is that you can write code which mostly looks like its synchronous code. We expect to be able to catch specific exception types as normal. Asynchronous code may potentially have multiple exceptions "at the same time". The … Continue reading Eduasync part 11: More sophisticated (but lossy) exception handling

Eduasync part 10: CTP bug – don’t combine multiple awaits in one statement…

(This post covers project 12 in the source code.) Last time, we saw what happens when we have multiple await expressions: we end up with multiple potential states in our state machine. That’s all fine, but there’s a known bug in the current CTP which affects the code generated when you have multiple awaits in the same statement. (Lucian Wischik calls these "nested awaits" although I personally don’t really think of one as being nested inside an another.) This post is mostly a cautionary tale for those using the CTP and deploying it to production – but it’s also interesting … Continue reading Eduasync part 10: CTP bug – don’t combine multiple awaits in one statement…

Eduasync part 9: generated code for multiple awaits

Last time we looked at a complex async method with nested loops and a single await. This post is the exact opposite – the method is going to look simple, but it will have three await expressions in. If you’re glancing down this post and feel put off by the amount of code, don’t worry – once you’ve got the hang of the pattern, it’s really pretty simple. Here’s the async method we’re going to analyze: private static async Task<int> Sum3ValuesAsyncWithAssistance() {     Task<int> task1 = Task.Factory.StartNew(() => 1);     Task<int> task2 = Task.Factory.StartNew(() => 2);     Task<int> task3 = Task.Factory.StartNew(() => … Continue reading Eduasync part 9: generated code for multiple awaits

Eduasync part 8: generated code from a complex async method

Last time we had a really simple async method, and looked at the generated code. Even that took a little while… but in this post I’ll demonstrate some of the intricacies that get involved when the async method is more complex. In particular, this time we have: A parameter to the async method A "using" statement, so we’ll need a finally block Two loops The possibility of catching an exception generated from await and retrying I’ve not made it as complicated as I could have done, mind you – we’ve still only got one await expression. Here’s the async method … Continue reading Eduasync part 8: generated code from a complex async method

Eduasync part 7: generated code from a simple async method

In part 6 we tried to come up with a "manual" translation of a very simple async method. This time we’ll see what the compiler really generates. As a reminder, here’s the async method in question: private static async Task<int> ReturnValueAsyncWithAssistance()  {      Task<int> task = Task<int>.Factory.StartNew(() => 5);      return await task;  } Whereas our manual code still ended up with a single method (admittedly involving a lambda expression), the generated code is split into the method itself (which ends up being quite small), and a whole extra class representing a state machine. The "stub" method We looked at a similar translation … Continue reading Eduasync part 7: generated code from a simple async method

Eduasync part 6: using the infrastructure manually

Now that we’ve got the infrastructure for both returning a task from an async method, and awaiting a Task<T>, we’re going to look at what the compiler does for us. I always find that the best way of appreciating a feature like this is to consider what we’d have to do without it. Later on we’re going to see what the C# compiler does in terms of decompiling the actual generated code, but first we’ll look at what we might do to achieve the same end result, using the same infrastructure. Of course, if we were doing all of this … Continue reading Eduasync part 6: using the infrastructure manually

Eduasync part 5: making Task<T> awaitable

In part 3 we looked at what the C# 5 compiler required for you to "await" something. The sample used a class which actually had an instance method called GetAwaiter, but I mentioned that it could also be an extension method. In this post, we’ll use that ability to make Task<T> awaitable – at which point we have everything we need to actually see some asynchronous behaviour. Just like the last part, the code here is pretty plain – but by the end of the post we’ll have a full demonstration of asynchrony. I should make it clear that this … Continue reading Eduasync part 5: making Task<T> awaitable

Eduasync part 4: Filling in AsyncTaskMethodBuilder<T>

In part 2, I introduced AsyncVoidMethodBuilder, AsyncTaskMethodBuilder and AsyncTaskMethodBuilder<T>. I showed all the signatures, but the implementations were basically trivial and non-functional. Today, I’m going to change all of that… at least a bit. In particular, by the end of this post we’ll be able to make the following code actually print out 10 as we’d expect: private static void Main(string[] args) {     Task<int> task = Return10Async();     Console.WriteLine(task.Result); } private static async Task<int> Return10Async() {     return 10; } Note that there are no await statements – remember that AsyncTaskMethodBuilder<T> is about the boundary between the caller and the async method. We … Continue reading Eduasync part 4: Filling in AsyncTaskMethodBuilder<T>

Eduasync part 3: the shape of the async method / awaitable boundary

Last time we looked into the boundary between the caller of an async method and the method itself. This time I’m going to show the same sort of "skeleton API" but for awaitable types. This is at the heart of C# 5’s async feature: within an async method, you can include "await" expressions. You have to await a value – which could be the return value from a method call, or a property, or the value of a variable. The important thing is that the compile-time type of that expression has the right shape. Definition time Consider this code snippet … Continue reading Eduasync part 3: the shape of the async method / awaitable boundary

Eduasync part 2: the shape of the caller / async method boundary

For the first few parts of this blog series, we’re not going to write code which actually does anything useful. We’re looking at the API more than the implementation – the bare necessities to get the code to compile. Remember that we’re working without AsyncCtpLibrary.dll – which of course supplies all the necessary types normally. In this part, we’ll look at the boundary between the caller and the async method itself. Our test code will have a method which does nothing but return – but which is marked with the “async” modifier. The fact that we don’t have an “await” … Continue reading Eduasync part 2: the shape of the caller / async method boundary