error: Null Value & Object reference not set...

  • Thread starter Thread starter DaveC
  • Start date Start date
D

DaveC

I have a simple one form VB.NET desktop app, that does a little threading
like so

mThread = New System.Threading.Thread(AddressOf DoSearch)
mThread.Start()

mThread has been decleared with the form class
Dim mThread As System.Threading.Thread

The DoSearch sub is in the same class as my form, and this sub is adding
and changing values of controls on the form. I think it may be because of
this I'm getting the following errors.

Public Class >Form1<
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.

Public Class >Form1<
An unhandled exception of type 'System.ArgumentNullException' occurred in
system.windows.forms.dll
Additional information: Value cannot be null.

It seems quite random.. The same search, with the same results may or may
not produce either of those errors or none at all?

Like I said.. My first though is that the thread some times just can not
find the form object class but I cant trace back in the code.. it always
brakes on that line "Public Class Form1"

Any ideas?

Thanks
DaveC
 
DaveC said:
I have a simple one form VB.NET desktop app, that does a little threading
like so

mThread = New System.Threading.Thread(AddressOf DoSearch)
mThread.Start()

mThread has been decleared with the form class
Dim mThread As System.Threading.Thread

The DoSearch sub is in the same class as my form, and this sub is adding
and changing values of controls on the form. I think it may be because of
this I'm getting the following errors.

Yes, I would concur.

It seems quite random.. The same search, with the same results may or may
not produce either of those errors or none at all?

Any ideas?


Code that effects any of the controls should be run from the main UI thread.
All that type of control accessing code should be moved out of your DoSearch
routine into a separate sub that you can call from the DoSearch routine.

Look into using the form's Invoke method to call out from DoSearch, to the
sub containing the control access code. Invoke will handle the cross-thread
issuses for you....

LFS
 
Back
Top