Gopher2011
Well-known member
Ok, after a long time trying to get one thread to talk to another thread, the whole thing was driving me nuts but its quite simple so I decided to put the code here to help people like me.
I know there are longer better more complex more complete examples there but I just wanted to change a label on another thread, not get men to the moon so please do forgive me if this is simple to some of you. The stop button does not do anything, but if the main thread was in a loop the code would have an avenue to escape the loop. I will cover this next.
Ok lets go
VB.net 2010 installed. This apparently only works in 2010 version [so I am told]. Upgrade ok.
New windows form project. Call the new form Main.vb Add 5 new objects listed below to the form.
Add a label - name is 'Label1'
Add a label - name is 'Label2'
Add a button - name is 'btn_Start'
Add a button - name is 'btn_Stop'
Add a timer - name is Timer1
Now copy this code to the new form you have created.
Now you can click run then when the form opens click start. You will get a '......' line increasing by another '.' every second. The Label2 goes invisible (to fast to see it), the thread pauses 2 seconds then the Label2 becomes visible with new text.
That is how you change form objects from another thread, nice and simple for
dummy's...
To prove my point if you add into the module
You will get an error after hitting the 'start' button when it tries to execute the code you will get a
System.InvalidOperationException Error.
{"Cross-thread operation not valid: Control 'Label2' accessed from a thread other than the thread it was created on."}
Have fun with it!
I know there are longer better more complex more complete examples there but I just wanted to change a label on another thread, not get men to the moon so please do forgive me if this is simple to some of you. The stop button does not do anything, but if the main thread was in a loop the code would have an avenue to escape the loop. I will cover this next.
Ok lets go
VB.net 2010 installed. This apparently only works in 2010 version [so I am told]. Upgrade ok.
New windows form project. Call the new form Main.vb Add 5 new objects listed below to the form.
Add a label - name is 'Label1'
Add a label - name is 'Label2'
Add a button - name is 'btn_Start'
Add a button - name is 'btn_Stop'
Add a timer - name is Timer1
Now copy this code to the new form you have created.
VB.NET:
Imports System.ComponentModel
Public Class Main
Private WithEvents NewThread As BackgroundWorker
Public Sub New()
InitializeComponent() 'default added by windows
NewThread = New BackgroundWorker 'Initiate new thread
Debug.WriteLine("NewThread will let other methods be aware of it")
NewThread.WorkerReportsProgress = True
Debug.WriteLine("NewThread will allow cancelation")
NewThread.WorkerSupportsCancellation = True
'This shows the main form thread constantly adding a '.' to Label1
Timer1.Enabled = True
End Sub
Private Sub btn_Start_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btn_Start.Click
Debug.WriteLine("btn_Start_Click")
NewThread.RunWorkerAsync() 'Run background thread
End Sub
Private Sub btn_Stop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btn_Stop.Click
Debug.WriteLine("btn_Stop_Click")
NewThread.CancelAsync() 'Stop thread
End Sub
Private Sub NewThread_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles NewThread.DoWork
Debug.WriteLine("NewThread_DoWork")
'Makes a change on the main form thread from thread NewThread
If Label2.InvokeRequired Then Label2.Invoke(Sub() Label2.Text = "New Label 2 Text")
'Makes a change on the main form thread from thread NewThread
If Label2.InvokeRequired Then Label2.Invoke(Sub() Label2.Visible = False)
'Demonstrates thread NewThread sleep is independant of teh main thread
System.Threading.Thread.Sleep(2000)
MessageBox.Show("I was sleeping for 2 seconds")
'Makes a change on the main form thread from thread NewThread
If Label2.InvokeRequired Then Label2.Invoke(Sub() Label2.Visible = True)
End Sub
Private Sub NewThread_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles NewThread.ProgressChanged
Debug.WriteLine("NewThread_ProgressChanged")
End Sub
Private Sub NewThread_RunWorkerCompleted(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles NewThread.RunWorkerCompleted
Debug.WriteLine("NewThread_RunWorkerCompleted")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Label1.Text & "."
If Label1.Text.Length > 40 Then Label1.Text = "."
End Sub
End Class
Now you can click run then when the form opens click start. You will get a '......' line increasing by another '.' every second. The Label2 goes invisible (to fast to see it), the thread pauses 2 seconds then the Label2 becomes visible with new text.
That is how you change form objects from another thread, nice and simple for
dummy's...
To prove my point if you add into the module
VB.NET:
Private Sub NewThread_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles NewThread.DoWork
'other text missed out for clarity
Label2.Text = "New label"
End Sub
You will get an error after hitting the 'start' button when it tries to execute the code you will get a
System.InvalidOperationException Error.
{"Cross-thread operation not valid: Control 'Label2' accessed from a thread other than the thread it was created on."}
Have fun with it!
Last edited: