Answered by:
Async call in loop

Question
-
Consider the below scenario where I need to call an async method for each item of an array. How can I ensure that the async call is made for each item of the array and each iteration runs in proper order. say for example, the async metod for the second item should not happen before completing the asyn call for the first item. Below is just sample code:
for( i = 0; i< n; i++ )
{
task<bool>CallMyAsync()( []( int x ) // for item i
});
}
Thursday, August 16, 2012 5:36 PM
Answers
-
task has a .get() method you can call which blocks until task completion -- but you can't run this loop on the UI thread as it's blocking.
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Thursday, August 16, 2012 8:39 PMModerator -
Hello,
As far as I know, you can use task group for many async function.
http://msdn.microsoft.com/en-us/library/windows/apps/dd492427.aspx#task_groupsOr you can use .then function to du something after front function finish.
Create_task([]{ // handle item 1}).then([]{// handle item 2}).then();If the codes in task group is simple, you use parallel_for
http://msdn.microsoft.com/en-us/library/dd728073.aspxBest regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Friday, August 17, 2012 6:43 AM -
Hi,
You can have a look at "create_iterative_task " in the concurrency::extras namespace. You can see it at work in the sample at http://code.msdn.microsoft.com/windowsapps/Windows-8-Asynchronous-08009a0d
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Friday, August 17, 2012 10:10 AMModerator
All replies
-
task has a .get() method you can call which blocks until task completion -- but you can't run this loop on the UI thread as it's blocking.
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Thursday, August 16, 2012 8:39 PMModerator -
Hello,
As far as I know, you can use task group for many async function.
http://msdn.microsoft.com/en-us/library/windows/apps/dd492427.aspx#task_groupsOr you can use .then function to du something after front function finish.
Create_task([]{ // handle item 1}).then([]{// handle item 2}).then();If the codes in task group is simple, you use parallel_for
http://msdn.microsoft.com/en-us/library/dd728073.aspxBest regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Friday, August 17, 2012 6:43 AM -
Hi,
You can have a look at "create_iterative_task " in the concurrency::extras namespace. You can see it at work in the sample at http://code.msdn.microsoft.com/windowsapps/Windows-8-Asynchronous-08009a0d
- Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:39 AM
Friday, August 17, 2012 10:10 AMModerator