Asynchronously Filling List in random order
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Asynchronously Filling List in random order

  1. #1
    Join Date
    Nov 2006
    Posts
    357

    Post 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...

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Asynchronously Filling List in random order

    use array instead of a list

    Code:
    Image[] pictures = new Image[100];
    
    pictures[67] = YourLoadedPicture;

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    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.

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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..

  5. #5
    Join Date
    Nov 2006
    Posts
    357

    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...

  6. #6
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center