I have used various flavours of async task implementation.
* WP7 / async CTP / v3 VS 2010
* Win8 / VS 2012
* Azure / VS 2012
* SL / async targetting pack / VS 2012
I blogged about how i had issues with #async on #aszure and funnily enough i wasn’t the only one. As discussed in https://invokeit.wordpress.com/2012/11/07/windows-azure-and-async-task-mechanism-async/ if you use something like this
AsyncDataSourceObj aeo = new AsyncDataSourceObj(); protected async Task BuildFilmDataSet() { Films films = await aeo.GetFilmData(); foreach(var film in films) { Cinemas cinemas = await aeo.GetCinemaData(film); // do things // do more things } }
It doesn’t work and a little investigation showed me that i wasn’t doing anything wrong.
The moment await aeo.GetCinemaData(film);
gets called, the execution instead of awaiting moves to the next iteration of for
loop. Why in the world you would want that ? I am sure if you wanted that one would have used Parallel.For
instead.
For some reason, the creators thought that we clever people wouldn’t ever want to do a async call in for loop. I mean its not like its the most common loop usage etc…
The suggestion is to use Task.Yield()
(or TaskEx.Yield
) within for
loop
It however doesn’t always work. I have tried it with my last Silverlight + Async Targetting pack and its still the same. Infact I had to revert to event based async in that case.
Here’s a screen of azure sample in action