Click to See Complete Forum and Search --> : Asynchronous events and complex loading


Grofit
June 30th, 2009, 04:17 AM
Hey,

Im porting over a library i wrote for one of my apps but im just wondering the best way to go about doing it...

Originally there were 2 main loaders, lets say CollageLoader and ImageReferenceLoader, the first one being ImageReferenceLoader acted like a palette of images. It was an XML file with a list of images and a few other bits and bobs. Then it would load up all the images and store them internally within the object.

CollageLoader loaded up an object that contained many ImageReference objects, and it basically indexed them, so it would have a list of reference_images giving an image reference object id (the image reference object to lookup) then an index, which was the index within that object that the image was stored.

Now this was fairly straight forward using non async methods:

ImageReferenceLoader
- Load XML
- Parse XML
- Load Images referenced
- Populate internal images with loaded ones
- return populated ImageReference

CollageLoader
- Load XML
- Parse XML
- Load ImageReferences via ImageReferenceLoader
- Populate internal ImageReferences with loaded ones
- return populated Collage


Now with Async methods (im using WebClient and OpenReadAsync/DownloadStringAsync) it is a whole new ballgame, so im currently thinking:

ImageReferenceLoader
- Start async string download
- download complete event fires
- Parse XML
- Start async open of images
- download event fires for each image (need some way of linking images to indexes, could use UserState with an index)
- Somehow check that all images have loaded
- fire CompletedEvent containing the populated object

CollageLoader
- Start async string download
- download complete event fires
- Parse XML
- Start ImageReferenceLoader for each ImageReference (need some way of linking these ImageReferences to the internal objects)
- Somehow check that all imagereferences have loaded
- fire CompletedEvent containing the populated object


Now as you can see i still have a few bits where im not quite sure how is the best way to proceed, so does anyone have any nuggets of greatness that could point me in the right direction?

Loading the ImageReferences shouldnt be too hard but im not sure how im going to load the CollageLoader as i wont know what ImageReference goes where, unless i give it a tag of some kind, but then im breaking the model for this bit of logic, or i give each loader an internal count to keep adding to as things load, but although i can tell when things load that way, i wont know which order they loaded...

Any advice would be great!

Grofit
July 1st, 2009, 03:11 AM
Just a healthy bump, i may need to write some extended loaders that allow for UserStates on the objects so i can track what is loading what... as i cant see any way around it as firing off the imagereference's will just return a complete image reference so i need some sort of ID with it...

boudino
July 1st, 2009, 04:02 AM
Maybe a code sample would be useful, because the issue is not clear (at least for me). Whats the problem to wait for (all) started ImageLoaders are finished and than continue? You can wait for it on a WaitHandle. This (http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx) and this (http://en.csharp-online.net/CSharp_Delegates_and_Events%E2%80%94Asynchronous_method_calls) could help a bit.

Grofit
July 1st, 2009, 04:58 AM
Problem is the original logic was for winforms and was fine, but now im porting into silverlight, and unfortunatly its not as flexible so you have to do everything async and i dont think there is any *good* way to block the threads until something has loaded...

I would post the code but there is alot of it and its slightly different to the scenario mentioned, i just simplised the problems im having here...

Basically its 2 async object that fire off complete events, however 1 of the objects contains multiple of the other objects, and you should only fire the complete events for each object when it is fully populated...

So as i said before the ImageReferenceLoader would fire a complete when it has parsed the XML and all the images have been loaded.

The CollageLoader would fire a complete when it has parsed the XML and loaded each one of its ImageReferenceLoaders.