background preloader

C# and Windows Programming

Facebook Twitter

Coding Instinct: Sequential Async using CoRoutines. If you work with WPF or Silverlight then you really should check out Rob Eisenberg presentation at MIX10 titled Build Your Own MVVM Framework.

Coding Instinct: Sequential Async using CoRoutines

In that presentation he goes through a sample application that utilizes no code behind and no data binding or command binding expressions, all is hooked up through conventions and is driven solely from the presentation model. The sample app has about 500 lines of framework or infrastructure code that handles the view look up and convention based binding. I urge you to download it and really dig through it, it contains some awesome code and ideas.

But I will focus this blog post on one aspect of that sample app which is how Rob implemented async workflows. It might be a long post with a lot of code :) Sequential async workflow: Before I dig into what the above code does and how it works I need to explain the problem. Example: This does not look so bad, we still have all the code in one method thanks to the lambdas. Coroutines to the rescue. Advanced Coroutines (inner workings) Motivation You should read this article if you really want to know what is going on behind the scenes in coroutines.

Advanced Coroutines (inner workings)

It will be very useful if you are following the FSM tutorial and you want to get the grimy details on how we can create interruptable coroutines that can be later resumed. This is an advanced subject and you don't really need to know this if you just want to use coroutines. If you are building systems and frameworks, or you are just curious then read on! If you want to know when coroutines are called and how to use them to create powerful sequences then you should read the Coroutines article, that's really got some useful tools in it too. Coroutines So a coroutine is basically an interruptable routine that can later be resumed. It's obvious that yield is a part of the C# language - so what is it really for? Code Based Enumerators A while back the designers of the CLR decided that it would be a really good idea if you could enumerate over things that were created by code.

Coroutines with IEnumerable and yield return. There was a brief flurry of excitement about the fancy yield return syntax when the C# 2.0 specification was first published some years ago, and the idea of it being used for creating coroutines.

Coroutines with IEnumerable and yield return

This quickly died down, as far as I can see anyway. Either people forgot the idea, or it became such boringly standard practice that no one ever thinks to discuss it anymore. I don’t know which, but I think it deserves more attention either way. There were a few people at the time saying that it was a shame that C# appeared to only support coroutines in a special case where the function returns some weird IEnumerable<T> thing, and wouldn’t it have been better if it was more general? But that’s a misunderstanding. Aside: A word about threads. Implementation - Implementing coroutines in Java. Coroutines in C. By Simon Tatham Introduction Structuring a large program is always a difficult job.

Coroutines in C

One of the particular problems that often comes up is this: if you have a piece of code producing data, and another piece of code consuming it, which should be the caller and which should be the callee? Here is a very simple piece of run-length decompression code, and an equally simple piece of parser code: Each of these code fragments is very simple, and easy to read and understand. In many modern operating systems, you could do this using pipes between two processes or two threads. emit() in the decompressor writes to a pipe, and getchar() in the parser reads from the other end of the same pipe. In this article I offer a creative solution to this sort of structure problem. Rewriting The conventional answer is to rewrite one of the ends of the communication channel so that it's a function that can be called.

Of course you don't have to rewrite both of them; just one will do. And that's the point, really. Single-Threaded Apartments (COM) Using single-threaded apartments (the apartment model process) offers a message-based paradigm for dealing with multiple objects running concurrently.

Single-Threaded Apartments (COM)

It enables you to write more efficient code by allowing a thread, while it waits for some time-consuming operation to complete, to allow another thread to be executed. Each thread in a process that is initialized as an apartment model process, and that retrieves and dispatches window messages, is a single-threaded apartment thread. Each thread lives within its own apartment. Within an apartment, interface pointers can be passed without marshaling, and therefore, all objects in one single-threaded apartment thread communicate directly.

A logical grouping of related objects that all execute on the same thread, and therefore must have synchronous execution, could live on the same single-threaded apartment thread. The interprocess and interthread models are similar. All calls to an object must be made on its thread (within its apartment). C# - STAThread and multithreading.