G
Guest
Hi there,
I am new to threading/callbacks etc and am having some trouble that I hope
someone can help me with.
The below class seems to work ok until I handle the CallbackEvent and try to
update a control in the main form. I get:
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread
other than the thread it was created on."
I'm sure the answer is easy but i am having trouble getting my head around
it. I have looked at many examples using control.invoke etc but this is just
a class not a control.
What I would like is for the CallbackEvent to be raised from the main thread
not the background thread as i assume this is the issue that is causing the
above error.
Any help would be appreciated.
Thanks
My test class:
Public Class TestThreadCallback
Public Delegate Sub CallbackDelegate(ByVal x As String)
Public Event CallbackEvent As CallbackDelegate
Private _t As Threading.Thread
Sub CallmeBack(ByVal x As String)
RaiseEvent CallbackEvent(x)
End Sub
Sub StartWorker()
_t = New Threading.Thread(AddressOf Worker)
_t.Start()
End Sub
Sub StopWorker()
_t.Abort()
End Sub
Sub Worker()
Do
Threading.Thread.Sleep(2000)
CallmeBack("boo")
Loop
End Sub
End Class
I am new to threading/callbacks etc and am having some trouble that I hope
someone can help me with.
The below class seems to work ok until I handle the CallbackEvent and try to
update a control in the main form. I get:
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread
other than the thread it was created on."
I'm sure the answer is easy but i am having trouble getting my head around
it. I have looked at many examples using control.invoke etc but this is just
a class not a control.
What I would like is for the CallbackEvent to be raised from the main thread
not the background thread as i assume this is the issue that is causing the
above error.
Any help would be appreciated.
Thanks
My test class:
Public Class TestThreadCallback
Public Delegate Sub CallbackDelegate(ByVal x As String)
Public Event CallbackEvent As CallbackDelegate
Private _t As Threading.Thread
Sub CallmeBack(ByVal x As String)
RaiseEvent CallbackEvent(x)
End Sub
Sub StartWorker()
_t = New Threading.Thread(AddressOf Worker)
_t.Start()
End Sub
Sub StopWorker()
_t.Abort()
End Sub
Sub Worker()
Do
Threading.Thread.Sleep(2000)
CallmeBack("boo")
Loop
End Sub
End Class