Print Page | Close Window

foreach loop with async calls, last one to call onsuccess

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3738
Printed Date: 20-Sep-2025 at 11:59am


Topic: foreach loop with async calls, last one to call onsuccess
Posted By: siko
Subject: foreach loop with async calls, last one to call onsuccess
Date Posted: 24-Oct-2012 at 3:13am
Hi,
 
Sorry if the subject is confusing, but here's what I want and I'm a bit lost on how to accomplish that:
 
I have an OperationResult LoadAsync that does the following:
return Coroutine.StartParallel(InitializeCore)
                .AsOperationResult()
                .ContinueWith(
                    or => Coroutine.Start(InitializeApplicationStorage, op => op.OnComplete(onSuccess, onFail))
                              .AsOperationResult()
                              .Execute()
                );
the private IEnumerable<INotifyCompleted> InitializeCore() executes a few 
yield return EntityManager.insertentitytypehere.Where(q => predicatehere).ExecuteAsync();
When the above routine finishes, the InitializeApplicationStorage routine is executed and here lies my problem; I have to do something like this:
	foreach (var questionObject in qoWithImages)
            // ReSharper restore LoopCanBeConvertedToQuery
            {
                var o = questionObject;
 
                yield return EntityManager.InvokeServerMethodAsync(
                    typeName, methodName,
                    callBack =>
                    {
                        //omitted for brevity => writing the received byte[] to isolated storage
                    },
                    null,
                    new object[] { questionObject.image_name, questionObject.test_id }
                    );
            }
I want the main LoadAsync onSuccess to be called when the last callback has finished, with other words, to continue when all the async functions have completed.
 
How can I do that?
 
Thanks again!
 



Replies:
Posted By: mgood
Date Posted: 24-Oct-2012 at 11:16am
If I read this correctly, you simply nest the above two coroutines in an outer coroutine like so:

        public OperationResult LoadAsync(Action onSuccess, Action<Exception> onFail)
        {
            return Coroutine.Start(LoadAsyncCore, op => op.OnComplete(onSuccess, onFail))
                .AsOperationResult();
        }

        private IEnumerable<INotifyCompleted> LoadAsyncCore()
        {
            yield return Coroutine.StartParallel(InitializeCore);
            yield return Coroutine.Start(InitializeApplicationStorage);
        }



Posted By: siko
Date Posted: 24-Oct-2012 at 2:08pm
Oh Marcel!
 
I was almost there. Had the same thought, make LoadAsyncCore, two yield return... Just that I forgot to call it with Coroutine.Start... had still the old StartParallel in place.... :-(
 
Now with this in place => :-)))
 
Thank you so much, again!



Print Page | Close Window