#async task and the for loop bug

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


To do or not to do? that is the question

I am definitely not a pukka application developer 😦

I have to hold back everything from ranting about some of the reviews i get. People RTFM or at least read the description – how about just the first 2 lines ???? At times i wish i’d never released the damn thing in first place.. damn you Hermit Dave

Okay enough for today 🙂 Most frequent complain is that it doesn’t replace system keyboard. well microsoft hasn’t provided any public api’s to do that. I am seriously getting tired of the comments. So here is what i think i should do.

1) Release 2.0 next week or so.
2) Migrate to mango and release 2.1 or at least get ready.

3) Try and hack WP7. Not in real sense just get hold of non public api. Then try and implement Sliding Keyboard into a homebrew app (assuming microsoft wont allow it in marketplace). I have already started my search and on first impression (usually the wrong one) is that it shouldn’t be that bad.

Enough for today. I have to finish changes to layout systems as i have some 5 distinct layouts to cater to now. Addition of Nordic / Russian has made me rewrite some of the layout code. Hopefully will finish soon.