Hello Everyone,

Okay I have got probably one of the weirdest question you have seen in awhile. I have been working on a custom image selection form (much like a file selection, but I didn't like the default Microsoft one). This also includes me rebuilding a custom object, that references, has multiple lists, other custom object, ext, ext.

Right now I have problem with it blow out of range exceptions. But here's where it gets weird ... it shouldn't be. Yeah I know, you think I am wrong. But when it blows the error I check all the code's values, and there are no nulls or -1s, nothing.

Here's a snippet of code that is blowing the error

private void backgroundWorker1_DoWork (object sender, DoWorkEventArgs e)
{
    using (BackgroundWorker worker = sender as BackgroundWorker)
    {
        FolderImages CurrentImages = CurrentSettings.RetrieveImages(CurrentTheme.ImageCollections [CurrentSelected].ID); //gets the current FolderImages we are working with

And the function it's calling is this

public FolderImages RetrieveImages (string RetrieveID)
{
    return Images.Find(x => x.ID.Equals(RetrieveID));
}   

Now here's where it gets even more weird. This is not the first time this function is called. Upon loading the form, it's populated with data, including a ComboBox with the following code:

for (int i = 0; i < CurrentTheme.ImageCollections.Count; i++) //add all the file paths (by their name)
{
    comboBox1.Items.Add(CurrentSettings.RetrieveImages(CurrentTheme.ImageCollections [i].ID).Name);
}

So that is called fine, it has no problems, everything loads in properly. Notice how it's almost identical to the piece of code above.

NOW, I have found away to prevent the error, and it's really why I am coming here ... timing. Yes if I close the form and re-open it (which involve redeclaring a new value which is disposed on the closing the form) rather quickly, the error is blown ... never at the ComboBox bit but the background worker. Now, if I close it and wait some time (probably 10-15+ seconds I am guessing, I haven't exactly timed it), the error does NOT occur. I have tried it on multiple occurances and it has succeeded

I am 100% puzzled by this. What voodoo logic is causing this nightmare of headaches? Only thing I can think of is the BackgroundWorker, and somehow it's holding onto resources after the form is closed and disposed. I will continue to investigate (I have one theory to try), but I could use some help

UPDATE: Ran one test, had to wait 20 seconds to prevent the error (from when it occured, to when I re-opened the form)

FACEPALM!

Okay I think I found my problem, I was being an idiot. I was trying to race the thread. By the time the debugger hits this, the "CurrentSelected" value has been updated to the value of 1, however, when I first call to start the thread, the value is -1 (it's updated right after the thread is started).

So now my question is, how the heck did it work this way? First time in, never a problem, waiting 20 secs, never a problem.

All I can think is the initialization of the thread took longer as these points cause maybe the automatic GC cleaned something out but I have no clue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.