Don't quote me, but I don't think it's good practice to refer to specific
components on a form from a thread. It couples the thread class and the
form too closely together. The best way I've found to do this is to store a
reference to an instance of the form in the thread class and then whenever
you want to signal to the main form to do something, "Invoke" a delegate on
the form. As an example of what this might look like below. We are
basically marshalling a call from the calling thread onto the form thread to
execute some function, passing in parameters. This function can contain
code to make modifications to your forms and/or the controls on them. I
usually have delegates for signalling error conditions, success, failure,
starting and ending an operation, but it all depends on what you are doing
with it.
http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx
' Declare one or more delegates
Private Delegate Sub DoSomething_Delegate(...)
' Store a reference to an instance of the form you want to signal
Private m_Form as MainForm
......
......
' At some moment in the thread, invoke to the form.
Invoke_DoSomething (...)
.....
.....
' Invoke to the form, passing parameters (if required), via. a delegate.
Public Sub Invoke_DoSomething (...)
Dim Parameters(...) As Object
Parameters(0) = ...
Parameters(1) = ...
Parameters(2) = ...
Parameters(3) = ...
....
Try
m_Form.Invoke(New DoSomething_Delegate(AddressOf
m_Form.DoSomething_Invoked), Parameters)
Catch ex As Exception
' Something went titsup.
End Try
End Sub
' Create a method on the form called "DoSomething_Invoked"
Public Sub DoSomething_Invoked(....)
....
....Modify controls etc.
....
End Sub