Print Page | Close Window

Coroutine Question

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2347
Printed Date: 28-Mar-2025 at 3:51am


Topic: Coroutine Question
Posted By: BillG
Subject: Coroutine Question
Date Posted: 03-Dec-2010 at 1:53pm

I have a method which has my coroutine.start call in it. It calls the iterator routine. If the iterator routine calls another method is that new method automatically part of the coroutine or do I have to do another coroutine.start and basically have nested coroutines.

 
public void A()
{
        var x = Coroutine.Start(B());
 
 
}
 
public IEnumerable<INotifyCompleted> B()
{
      double a, b;
        var y = AddFunction(a, b);
       yield return Coroutine.Return(y);
}
 
public double CalculateFormula(double a, double b)           // is this part of the coroutine? Do I have to do a yield return?
{
      return a + b;
}



Replies:
Posted By: kimj
Date Posted: 03-Dec-2010 at 3:17pm
The serial Coroutine allows you to batch up async actions, but you can certainly intersperse synchronous logic and function calls within the iterator block.   In what you're showing here CalculateFormula is a synchronous call, so you can call it within your iterator block.  In this case "B" should also be doing something async, otherwise why bother with the Coroutine.
 
You only need to use yield return when "yielding" due to an async operation.  When you start an async operation you yield, and execution of the block resumes when the operation completes.  A synchronous call does not require a yield.
 
Also, the (optional) Coroutine.Return() result which you can return from an iterator block is not truly the same thing as the INotifyCompleted object which you yield return when awaiting the completion of the async operation.  The Return(x) is just a handy way to return a result back to the caller, it's returned to the Coroutine completion handler in the Result property of the event args.  So here, you could setup your call like this:
 
   var op = Coroutine.Start(B);
    op.Completed += (s,e) => {
         var result = e.Result;
    }
 
or this:
 
   Coroutine.Start(B, op => {
      var result = op.Result;
   });
 



Print Page | Close Window