New Posts New Posts RSS Feed: Coroutine Question
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Coroutine Question

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: Coroutine Question
    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;
}
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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;
   });
 


Edited by kimj - 03-Dec-2010 at 3:23pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down