Print Page | Close Window

Coroutine Start example

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=3400
Printed Date: 25-Apr-2024 at 7:44am


Topic: Coroutine Start example
Posted By: scottarlp
Subject: Coroutine Start example
Date Posted: 23-Apr-2012 at 12:36pm
I've apparently hit a dense point. I'm attempting to call multiple coroutine methods at once to keep from chaining the callbacks.
 
Given two methods like the following:
 
public IEnumerable<INotifyCompleted> DoThis(int x) { ... }
and
public IEnumerable<INotifyCompleted> DoThat() {...}
 
How do I construct (or can I) the list to call Coroutine.Start(...)? I was trying to create a IEnumerable<Func<INotifyCompleted>> list, but I'm not having any luck adding the methods.
 
Thanks for any help.
Scott



Replies:
Posted By: DenisK
Date Posted: 23-Apr-2012 at 2:06pm
Hi Scott,

Sounds like you might want to use Coroutine.StartParallel instead.  http://drc.ideablade.com/xwiki/bin/view/Documentation/parallel-async-coroutines - http://drc.ideablade.com/xwiki/bin/view/Documentation/parallel-async-coroutines


Posted By: esaulsberry
Date Posted: 23-Apr-2012 at 2:10pm
If I understand you correctly, you'll need to add a method for the .Start() method to call, like this:

priviate IEnumerable<INotifyCompleted> DoStuff()
{
yield return DoThis(int x) { ... }

yield return DoThat() {...}
}


Now call that from somewhere:

public void DoAsyncStuffForMe()
{
Coroutine.Start(GetStatusData);
}


You can also set up the caller to notify when it's done:
public INotifiyCompleted DoAsyncStuffForMe(Action onSuccess)
{
var op = Coroutine.Start(GetStatusData);
op.Completed += (s, a) =>
{
if(a.CompletedSuccessfully && onSuccess != null)
{
onSuccess();
}
return op();
}



Posted By: scottarlp
Date Posted: 24-Apr-2012 at 5:01am
Thanks for the replies. I need to run the two methods in serial (one is semi-dependent on the other), so I don't think parellel will do the trick. Correct me if I'm wrong here.
 
I tried esaulsberry's example, but the yield return DoThis(int x) { ... } portion inside DoStuff() gives a convert expression error. The method is a IEnumable list of INotifyCompleted and yield is expecting a single INotifyCompleted result.
 
From looking at the overloads on Coroutine.Start(), it appeared there was a way to create a ienumable list you could pass and it would fire each in serial, but I either can't figure out the type of list (docs show something like IEnumerable<Func<INotifyCompleted>>) or it doesn't do what I thought it does.
 
I can of course have a Coroutine.Start(...) of one method and then on the completed of it fire the next one, but that's just a couple steps away from outside Coroutine purpose to begin with (daisy callbacks).
 
 


Posted By: esaulsberry
Date Posted: 24-Apr-2012 at 7:44am
Scott,
Both DoThis and DoThat should be returning a single INotifyCompleted.  Are they?

If not perhaps seeing more of the guts of them would help.

Elton


Posted By: esaulsberry
Date Posted: 24-Apr-2012 at 8:00am
I think I see where you're going now.  DoThis and DoThat are themselves coroutine stacks?  In which case I think you'll need another level of wrapper.

        private INotifyCompleted CallChain()
        {
            var op = IdeaBlade.EntityModel.Coroutine.Start(Chain);
            op.Completed += (s, a) =>
                {
                    if (op.CompletedSuccessfully)
                    {
                        // do something
                    }
                };
            return op;
        }
        private IEnumerable<INotifyCompleted> Chain()
        {
            var op1 = IdeaBlade.EntityModel.Coroutine.Start(() => DoThis(x));
            yield return op1;
 
            var op2 = IdeaBlade.EntityModel.Coroutine.Start(DoThat);
            yield return op2;
        }


Posted By: scottarlp
Date Posted: 24-Apr-2012 at 10:23am
Thank you. That's what I needed. I've been trying to yield the method itself instead of the Coroutine.Start() inside the Chain() method and it just didn't dawn on me to nest the Coroutine.Starts inside there. I was hoping to be able to do without the Chain method, but that'll work great for now.
 
Scott


Posted By: DenisK
Date Posted: 30-Apr-2012 at 7:11pm
Hi Scott,

Referring to your original post, here's a post on our DRC showing how to do serial coroutines with function list.  http://drc.ideablade.com/xwiki/bin/view/Documentation/coroutine-function-list - http://drc.ideablade.com/xwiki/bin/view/Documentation/coroutine-function-list



Print Page | Close Window