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

Custom Coroutine Operation

 Post Reply Post Reply
Author
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Topic: Custom Coroutine Operation
    Posted: 16-Dec-2010 at 12:06pm

Is there an example on how to write a custom coroutine operation?  What I mean is a custom operation that implements INotifyCompleted or inherits from BaseOperation.  All of the documentation I've found so far shows how to write the producer and consumer, and the producer is always yield returning DevForce operations such as EntityQueryOperation or EntitySaveOperation.  We're looking to have our coroutine execute a custom web service as one of the operation steps but I'm having trouble determining how to implement INotifyCompleted.  There's only 1 method to implement.  It seems fairly clear that the WhenCompleted() method should be called when the web service finishes it's async processing, but who supplies the completedAction?  Something like this:

 

private IEnumerable<INotifyCompleted> CoroutineProducer()

{

                var op1 = em.Customers.Where().ExecuteAsync();

                yield return op1;

 

                var op2 =WebServiceExecutor.ExecuteAsync();

                yield return op2;

 

                var op3 = em.SaveChanges();

                yield return op3;

}

 

static public class WebServiceExecutor

{

                static public WebServiceOperation ExecuteAsync()

                {

                                var op = new WebServiceOperation();

                                op.Start();

 

                                return op;

                }

}

 

public class WebServiceOperation : INotifyCompleted

{

                public void Start()

                {

                                WebServiceClient client = new WebServiceClient();

                                client.SomeMethodCompleted += (completedArgs) =>

                                {

                                                WhenCompleted(/* but what Action do I pass? */ null);

                                };

                                client.SomeMethod();

                }

 

                public WhenCompleted(Action<INotifyCompletedArgs> completedAction)

                {

                                if (completedAction != null)

                                                completedAction(new WebServiceOperationCompletedArgs());

                }

}

 

I feel like I must be missing something obvious, but I can't seem to connect the dots.  Am I heading about this all wrong?

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: 20-Dec-2010 at 6:57pm

The WhenCompleted method is actually called by the Coroutine to pass you an action to call when your asynchronous operation completes.   You should hold on to that completedAction callback passed into WhenCompleted, and in your SomeMethodCompleted logic just call that action, pasing in the WebServiceOperationCompletedArgs (assuming they implement INotifyCompletedArgs).

If you're interested, there's a (more complicated) example of a custom Coroutine operation in the Bad Golf example in the cookbook - http://drc.ideablade.com/xwiki/bin/view/Documentation/Cookbook
Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 21-Dec-2010 at 7:32am
Ahh I see, that makes sense.  Thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down