New Posts New Posts RSS Feed: Coroutine StartParallel completed event fire only once
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Coroutine StartParallel completed event fire only once

 Post Reply Post Reply
Author
sarmaad View Drop Down
Newbie
Newbie


Joined: 18-Jul-2010
Location: Sydney
Posts: 15
Post Options Post Options   Quote sarmaad Quote  Post ReplyReply Direct Link To This Post Topic: Coroutine StartParallel completed event fire only once
    Posted: 03-Nov-2010 at 6:23am
I have the following code, executed every time the vm instantiated to pre-load some lists... 

var c = Coroutine.StartParallel(LoadLists);
c.Completed +=
    (sender, args) =>
        {
            if (args.CompletedSuccessfully)
            { [truncated] }
        };

it works as expected on the first instance of the class. But creating a second instance of the same class, the completed event does not fire.

the method "LoadLists" does run and the yield returns are executed as expected, but the event itself does not fire...

any thought.

thanks.
Back to Top
jsobell View Drop Down
Groupie
Groupie
Avatar

Joined: 02-Apr-2009
Location: Australia
Posts: 80
Post Options Post Options   Quote jsobell Quote  Post ReplyReply Direct Link To This Post Posted: 03-Nov-2010 at 3:39pm
I'm guessing here, as I found this a bit odd in the syntax of the Coroutine class, but if the results are cached and available immediately will 'c' not return immediately with completed data, hence running synchronously and not having any initial Completed action to call.

1. Create Coroutine
2. Coroutine calls to fetch data Async but returns immediately due to empty function or cached data?
3. Coroutine calls Completed (which is unassigned)
4. We set Completed handler
5. We wait for Completed event... forever!

I would have thought that Coroutine.StartParallel(LoadLists, Action<CompletedEventArgs>) would have made more sense, as I'm unclear as to how you can be sure that LoadLists will take any time at all :)

Just wondering, as I haven't experienced this issue or decompiled the function... yet...
Back to Top
sarmaad View Drop Down
Newbie
Newbie


Joined: 18-Jul-2010
Location: Sydney
Posts: 15
Post Options Post Options   Quote sarmaad Quote  Post ReplyReply Direct Link To This Post Posted: 03-Nov-2010 at 8:35pm
thanks for the reply...

I originally had my code using Coroutine.StartParallel(LoadLists, Action<CompletedEventArgs>), but changed it to test if it does make a deference... which it doesn't...

the issue here is this function is called once when the class is instantiated.
so the first instance, it runs with no issues... on the subsequent instances, the completed event does not fire..

eg:

var x = new Class(); <- the method is called and successfully invoke the completed event
var x1 = new Class(); <- the method is called but completed event does not fire

the only deference between the first instance and all others, is the data is returned from cache for all other instances...

even if the data are returned from cache, shouldn't completed event fire anyway?

below is the LoadLists method.

        IEnumerable<INotifyCompleted> LoadLists()
        {
            yield return _repository.GlobalManager.Products.Where(col => col.IsActive)
                .ExecuteAsync(opt =>
                {
                    if (opt.CompletedSuccessfully)
                    {
                        _manager.ImportEntities(opt.Results, MergeStrategy.OverwriteChanges);
                    }
                });

            yield return _repository.GlobalManager.ProductSuppliers
                .ExecuteAsync(opt =>
                {
                    if (opt.CompletedSuccessfully)
                    {
                        _manager.ImportEntities(opt.Results, MergeStrategy.OverwriteChanges);
                    }
                });
            yield return _repository.GlobalManager.Contacts
                .Where(col => col.IsActive && col.ContactType == ContactTypeEnum.SUPPLIER.ToString())
                .ExecuteAsync(opt =>
                {
                    if (opt.CompletedSuccessfully)
                    {
                        _manager.ImportEntities(opt.Results, MergeStrategy.OverwriteChanges);
                    }
                });
        }






Edited by sarmaad - 03-Nov-2010 at 8:36pm
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: 04-Nov-2010 at 12:52pm
We've confirmed this to be a bug.  As you've found the issue is not with the timing on when the completion handler is added, or whether the callback or Completed handler is used; neither will be called when all operations in the iterator complete synchronously.  We'll get this fixed in the next release (6.0.7). 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down