-
November 27th, 2008, 06:22 AM
#1
Asynchronously Filling List in random order
Hey there,
Ive got a problem with the current task im working on, and im not sure how is the best way to solve it.
Basically i have an asynchronous loader that loads images from an XML file, so there could be 1 or 100 image links contained. Now to make it slightly harder they need to maintain the order that they are on the XML file. The Images are contained within an object that is partially populated from the XML data, then it has a list<Image> that should be filled with the loaded images as they complete.
So i thought the easiest way to do this would be to make sure that each image has an index of where it resides, so when its loaded it knows where to go in the list. So if there was 100 images and lets say number 67 completed first i was assuming it would just allow me to put it in there, and then as the others fly in it would eventually reach 100, just they get put inserted in a random order.
Now it loves to spit out an error telling me that:
"Index must be within the bounds of the List."
So im assuming that it will only let me insert into an already existing index in the list OR at the end of the list... so is my understanding correct, and if so is there any way to achieve what i want?
I was thinking about maybe having a loop so if you try to insert at an index > list count then it would fill in dummy data to flesh out until that count, but it sounded a little inefficient, so just wanted to see if anyone could think of a better way...
-
November 27th, 2008, 06:47 AM
#2
Re: Asynchronously Filling List in random order
use array instead of a list
Code:
Image[] pictures = new Image[100];
pictures[67] = YourLoadedPicture;
-
November 27th, 2008, 06:55 AM
#3
Re: Asynchronously Filling List in random order
Thing is i dont know how many indexes there will be, there could be 1 or 100, and as the async loaders are set off before the XML has finished parsing you wont know how many indexes there are total until its too late, which is why im using a list as its either that or my own linked list...
Also further down the line there may be new images added after this point so it needs to be able to add and remove things at will, an array does not give that flexibility.
-
November 28th, 2008, 03:24 AM
#4
Re: Asynchronously Filling List in random order
when you open the xml document, you know how many node there are and thus how many images?
Code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("myImages.xml");
Image[] pictures = new Image[xmlDoc.DocumentElement.ChildNodes.Count];
pictures[67] = YourLoadedPicture;
Please post a sample of how your xml file looks like..
-
November 28th, 2008, 07:33 AM
#5
Re: Asynchronously Filling List in random order
basically like
Code:
<main_data someatt1="1" someatt2="2">
<parent_X someatt1="1" someatt2="2">
<child_X someatt1="1" someatt2="2" />
... Random Amount of Children
</parent_X>
.. Random Amount of Parents
</main_data>
Thats basically the layout of my XML, i have an unknown number of child nodes to the main node, then each one of them can have an unknown amount of nodes. An array would still not be flexible enough to add elements later on, so again i cannot use a default array unless i write some dynamic wrappers for it (a list<>, which already exists).
Ive currently solved it by adding a null element each time an async is fired off, so there is always a relvent index available. At least that way there is minimal alteration to the code flow, and i dont have to do any additional traversing of the XML hierarchy...
-
November 28th, 2008, 08:04 AM
#6
Re: Asynchronously Filling List in random order
Maybe use an Hashtable, wich contains a (unique) key and a value, but then you image should have some kind of ID or number.
Code:
Hashtable htPictures = new Hashtable();
htPictures.Add(67, yourLoadedPicture);
after asynchrone loading is complete
Code:
for (int i = 0; i < htPictures.Count; i++) {
Image picture = htPictures[i];
//and do something with you picture
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|