sharing data between forms with background worker

hshawa

Member
Joined
Aug 2, 2012
Messages
8
Programming Experience
1-3
Hi guys!

I am having an issue sharing a variable between 2 forms. The first form populates this variable, which is an array, using a backgroundworker.

This background worker is responsible for populating the array, after it finishes the first form loads a second form that is required to use the information in the array.

Now, unfortunately, this is not possible because the copy of the array that form2 gets is null. I have tried several things to fix it, one of them is to put populating the array in the bakcgroundWorkerReportsProgress event handler of form 1, also tried to pass the array to the constructor of the second form, but still it gets a null array and none of the values are there.

I just need a way to share this variable, which is globally declared, between 2 forms given that it is created in a backgroundWorkerThread.

Any tips?

Thanks
 
There's nothing to it. You set the variable on one for and you get it on the other. That's it. If it's not working for you then you're doing it wrong. If we can't see what you're doing then we can't see what's wrong with it.
 
Hi jmcilhinney,

Somewhere in form1, in the backgroundworker_DoWork event handler, the array called intervals is populated using a loop. After that is done, form2 is loaded like this (from within the DoWork handler)

VB.NET:
[COLOR=#0000ff]Dim[/COLOR] frm2 [COLOR=#0000ff]As[/COLOR] [COLOR=#8515ea]New[/COLOR] Form2
        frm2.ShowDialog()

now form2 uses this array to build a simple table, like this:

VB.NET:
[COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Class[/COLOR] Form2
    [COLOR=#0000ff]Function[/COLOR] getTable() [COLOR=#0000ff]As[/COLOR] DataTable

        [COLOR=#0000ff]Dim[/COLOR] table1 [COLOR=#0000ff]As[/COLOR] [COLOR=#8515ea]New[/COLOR] DataTable

        table1.Columns.Add("# of users", [COLOR=#0000ff]GetType[/COLOR]([COLOR=#6f002f]Integer[/COLOR]))
        table1.Columns.Add("delay in milliseconds", [COLOR=#0000ff]GetType[/COLOR]([COLOR=#6f002f]Long[/COLOR]))
               

 [COLOR=#0000ff]For[/COLOR] t = 0 [COLOR=#0000ff]to[/COLOR] testingCases - 1
           
table1.Rows.Add(users, intervals(t))
            
users = users + increments
                   [COLOR=#0000ff]Next[/COLOR] 

        [COLOR=#0000ff]Return[/COLOR] table1

    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Function[/COLOR]

      
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Class[/COLOR]

It stops exactly at the line: table1.Rows.Add(users, intervals(t))
putting a try catch block around it, shows an error of: object reference not set to an instance of an object. This is why i am suspecting that intervals array is not passed, because it is a null valued array.

Any ideas?

Thanks
 
After that is done, form2 is loaded like this (from within the DoWork handler)
Show your forms in UI thread only.
am suspecting that intervals array is not passed
I can't see you're passing anything anywhere.
 
Absolutely.
I haven't seen what you have tried, so I can't comment on that, but an array object can't suddenly become Nothing while there's still a reference held to it.
 
Thanks again for your input, finally it is working.

This is what I did:
put the loading of form2 out of the backgroundworker thread, and instead in the RunWorkerCompleted event handler. That didn't work alone, with the same error,
so i passed the array by reference to the constructor of form2 (I also noticed that I had another variable called intervals lingering around in form 1 so i removed it in order to pass the correct one in form2(intervals) ), then it worked.

Out of curiousity i tried all different possibilities, and turns out the reason was this long type array that was in form1 with the same name (intervals). once this line was commented, I could put loading form2 in the backgroundWorker_DoWork or whereever, I could pass the array or not, but it would work. So that was the problem.

Thanks!
 
Back
Top