Another thread problem... Cross-thread operation not valid (Resolved)
Does anyone know why I get this error?
Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
It happens when I try to modify TextBox1's properties from a thread I created.
The thread is a private variable in a class which raises an event. The handler of the event raised is where I tried to modify TextBox1.
Code used to create thread
sckTick = New Thread(AddressOf CallbackTick)
sckTick.Start()
Code of event handled, in thread sckTick
Private Sub sckMainConnected() Handles sckMain.Connected
MsgBox("Connected", MsgBoxStyle.Information, "")
TextBox1.Enabled = True
End Sub
Any ideas what I'm doing wrong?
Re: Another thread problem... Cross-thread operation not valid
Have a look at the help topic for the Control.Invoke method for details on how to call methods of controls across thread boundaries.
Re: Another thread problem... Cross-thread operation not valid
Delegate Sub FormControlsSub()
Protected Sub EnableTxtBox()
TextBox1.Enabled = True
End Sub
Private Sub sckMainConnected() Handles sckMain.Connected
MsgBox("Connected", MsgBoxStyle.Information, "")
Dim MSD As FormControlsSub
MSD = AddressOf EnableTxtBox
MSD.Invoke()
End Sub
Still get the same problem, this time, in EnableTextBox
Edit: Got it
Delegate Sub FormControlsSub()
Protected Sub EnableTxtBox()
TextBox1.Enabled = True
End Sub
Private Sub sckMainConnected() Handles sckMain.Connected
MsgBox("Connected", MsgBoxStyle.Information, "")
Dim MSD As FormControlsSub
MSD = AddressOf EnableTxtBox
Me.Invoke(MSD)
End Sub