Eduasync part 14: Data passing in coroutines

(This post covers project 19 in the source code.) Last time we looked at independent coroutines running in a round-robin fashion. This time we’ll keep the round-robin scheduling, but add in the idea of passing data from one coroutine to another. Each coroutine will act on data of the same type, which is necessary for the scheme to work when one coroutine could "drop out" of the chain by returning. Designing the data flow It took me a while to get to the stage where I was happy with the design of how data flowed around these coroutines. I knew … Continue reading Eduasync part 14: Data passing in coroutines

Eduasync part 13: first look at coroutines with async

(This part covers project 18 in the source code.) As I mentioned in earlier parts, the "awaiting" part of async methods is in no way limited to tasks. So long as we have a suitable GetAwaiter() method which returns a value of a type which in turn has suitable methods on it, the compiler doesn’t really care what’s going on. It’s time to exploit that to implement some form of coroutines in C#. Introduction to coroutines The fundamental idea of coroutines is to have multiple methods executing cooperatively, each of them maintaining their position within the coroutine when they yield … Continue reading Eduasync part 13: first look at coroutines with async

Eduasync part 12: Observing all exceptions

(This post covers projects 16 and 17 in the source code.) Last time we looked at unwrapping an AggregateException when we await a result. While there are potentially other interesting things we could look at with respect to exceptions (particularly around cancellation) I’m just going to touch on one extra twist that the async CTP implements before I move on to some weird ways of using async. TPL and unobserved exceptions The Task Parallel Library (TPL) on which the async support is based has some interesting behaviour around exceptions. Just as it’s entirely possible for more than one thing to … Continue reading Eduasync part 12: Observing all exceptions

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

LINQ To Objects and the performance of nested "Where" calls

This post came out of this Stack Overflow question, which essentially boils down to which is better out of these two options: var oneBigPredicate = collection.Where(x => Condition1(x)                                          && Condition2(x)                                          && Condition3(x)); var multiplePredicates = collection.Where(x => Condition1(x))                                    .Where(x => Condition2(x))                                    .Where(x => Condition3(x)) The first case is logically a single "wrapper" sequence around the original collection, with a filter which checks all three conditions (but applying short-circuiting logic, of course) before the wrapper will yield an item from the original sequence. The second case is logically a set of concentric wrapper sequences, each applying a … Continue reading LINQ To Objects and the performance of nested "Where" calls

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…