Print Page | Close Window

Coroutine StartParallel completed event fire only once

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=2280
Printed Date: 19-Oct-2025 at 12:22pm


Topic: Coroutine StartParallel completed event fire only once
Posted By: sarmaad
Subject: Coroutine StartParallel completed event fire only once
Date 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.



Replies:
Posted By: jsobell
Date 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...


Posted By: sarmaad
Date 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);
                    }
                });
        }






Posted By: kimj
Date 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). 



Print Page | Close Window