Do we need coroutines with the new async framework

Oct 30, 2010 at 11:09 AM

Just watched the future of C# on PDC10 and i liked the session.

So there will be new keywords (available in CTP) for async methods and statements like await to handle completion.

Under water the compiler will redesign the structure program to handle the ascync stuff.

I guess this will make life more simple and the IResult implemention could be replaced by the new asyc framework.

Coordinator
Oct 30, 2010 at 5:32 PM

In the next version of C#, Caliburn.Micro will remove its IResult stack :)  It's just a hack that was implemented until they added it to the language :)

Nov 30, 2010 at 8:44 AM

Hello,

I would like to know how to use async methods with CM. Can anyone show me some example code or point me
to a tutorial? I've read the acrticle IResult and coroutines but I doesn't know where to begin, what is essential?

 

 

Nov 30, 2010 at 2:24 PM

Just to clarify: IResult and coroutines are not intended to be used with async stuff (which presently is only available in CTP).

IResult are mainly aimed to encapsulate a long async operation in a class, so that it could be "started" by the framework at a certain point in time, after the user has properly configured it (they are basically built around Command pattern).
So, if you have an async service call, or you have an operation that spawns multiple thread, to call it in a coroutine you should:

  • wrap it into an IResult implementation
  • start the operation on Execute body
  • raise the Completed event when the operation actually ends

Some general purpose IResult implementation are available in Caliburn source code (ShellFramework project).

Coroutines are a basically a way to delay the configuration of the next IResult to be executed at the latest time.
The framework expects an ordered set of IResult to be executed, one at time (IEnumerable<IResult>).
You *might* simply return an array or a list, but you have to know which "steps" to call (i.e. which fully-configured IResult instances to return) in advance.
Using a C# iterator allows you to decide the next step in the sequence based on the result of the previous one, thus allowing a simple imperative programming in routines containing asyncronous parts.

I don't have a specific sample at hand, but this Rob's article could definitely help (it refers to Caliburn, not CM, but the call syntax is identical): http://devlicio.us/blogs/rob_eisenberg/archive/2009/07/10/asynchronous-execution-animation-and-more-in-mvvm-with-caliburn.aspx