accessing a control or a property of an object that are created in a diffrent thread
How is it possible to access a control (f. ex. changing the text property of a Label) or a property of any custom object, when the control or custom-object are created in a diffrent thread than the one from where I want to update them?
Here is an example of what does not work :
VB Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
StartDoWeighings()
End Sub
Dim Thread As System.Threading.Thread = New System.Threading.Thread(AddressOf Me.ThreadSub)
Private Sub StartDoWeighings()
If Not Thread.IsAlive Then
Thread.Start()
End If
End Sub
Sub ThreadSub()
Label1.Text = Date.Now.ToLongTimeString
End Sub
End Class
And here is the error when the button is clicked :
"Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on."
Thank you very much for any help !
Regards,
Fabian
Re: accessing a control or a property of an object that are created in a diffrent thread
Although you weren't supposed to, you were allowed to do this in .NET 1.1. .NET 2.0 forbids this type of cross-thread call, so you need to call Invoke using a Delegate. Have a look in the help for the Control.Invoke method for more info.
Re: accessing a control or a property of an object that are created in a diffrent thr
Hi jmcilhinney,
I could have seached for months to find this out...
Thank you very much for this great help !
Regards,
Fabian