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

Coroutine Start example

 Post Reply Post Reply
Author
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Topic: Coroutine Start example
    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
Back to Top
scottarlp View Drop Down
Newbie
Newbie


Joined: 07-Apr-2011
Posts: 26
Post Options Post Options   Quote scottarlp Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
esaulsberry View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post 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;
        }
Back to Top
esaulsberry View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
scottarlp View Drop Down
Newbie
Newbie


Joined: 07-Apr-2011
Posts: 26
Post Options Post Options   Quote scottarlp Quote  Post ReplyReply Direct Link To This Post 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).
 
 
Back to Top
esaulsberry View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Oct-2011
Location: Atlanta
Posts: 9
Post Options Post Options   Quote esaulsberry Quote  Post ReplyReply Direct Link To This Post 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();
}

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
scottarlp View Drop Down
Newbie
Newbie


Joined: 07-Apr-2011
Posts: 26
Post Options Post Options   Quote scottarlp Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down