Question Access main form from second thread running in a module

dghervas

New member
Joined
Jun 22, 2010
Messages
4
Programming Experience
Beginner
I was a vb6 user, done a few projects only with forums help.
Now i am moving to vb.net(vb2010) and i have some trouble.

Lets say we have a form(form1), a module(mod1) and a textbox on the form1.

i want to start a Sub (DoWork) in the module but in a new thread.

Dim Thread1 As System.Threading.Thread
Thread1 = New Threading.Thread(AddressOf mod1.DoWork)
Thread1.Start()

ok so far?

The DoWork sub does some stuff and when it's finished i want to change the text of a textbox1 in the main form and start another sub (DoWork2) on the main form under the main thread not the one DoWork is running under.

Plese do not refer me to another post that mighe give me a part answer, try to explain to me what i have to do and why.


Thank you.
 
Well honestly on the form(form1) I would use a Backgroundworker component to do my threading. You can set properties on the Backgroundworker to allow it to update the main thread as well as to allow cancellation. The Backgroundworker has events that can be called to perform your separate work (interestingly there is the DoWork() event) and will help to perform updates (the ProgressChanged event) as well as help deal with what needs to be done when the separate process is done running (RunWorkerCompleted event).

More info on the Backgroundworker.


Also you may need to use delegates to invoke methods on the main thread from inside the background worker thread. This is an excellent start for delegates .

This is an excellent tutorial using a backgroundworker and a delegate.

I know you said you didn't want links, but the best way to learn / understand is to do some research.
 
Thank you demausdauth.
I dont mind doing reasearch, i have been doing it for a month on this subject.
I managed to do it but i dont know if it's the best way.
Anyway i have another problem that i think is related to this thread.

I have the main form and a textbox on it.
In a separate class module i am runing a a sub in a separate thread
in this sub i need the text property of the textbox in the main form.
I have searched and searched and didnt find a working solution for my little problem.
The things that i tryed return the text that the textbox had when the form loaded, if i change it it still gets the "default" value.
Vb.net seems a lot more complicated the the old vb6. I know it has many advantages but a lot harder and more code to do something simple. Hope i will be able to learn the basics and get accustomed to it.

Thank you
 
Last edited:
I found the answer and here it is:

Sub Moo()
Dim instance As New Too
instance.NewProperty = "hello"
Dim th As New Threading.Thread(AddressOf instance.Foo)
th.Start()
End Sub

Class Too

Private newPropertyValue As String

Public Property NewProperty() As String
Get
Return newPropertyValue
End Get
Set(ByVal value As String)
newPropertyValue = value
End Set
End Property

Public Sub Foo()
MsgBox(Me.newPropertyValue)
End SubEnd Class
 

Latest posts

Back
Top