Private Sub SetText(ByVal str_text As String)
If Me.textbox1.InvokeRequired = True Then
'call an asyncronous invoke, by calling it then forcing a DoEvents
Dim asyncInvokeThread As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf AsyncInvoke))
asyncInvokeThread.IsBackground = True
asyncInvokeThread.Start(str_text)
Application.DoEvents()
Else
me.textbox1.text=str_text
End If
End Sub
Private Sub AsyncInvoke(ByVal obj_text As Object)
Dim str_text As String = CStr(obj_text)
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {str_message})
End Sub
- Published on
Thread.Join Deadlock with Invoke
- Authors
- Name
- Craig Wardman
If you are trying to Make Thread-Safe Calls to Windows Forms Controls then you can run into deadlock problems when calling 'Invoke' from your thread, if your main thread is waiting for a Thread.Join on the calling thread.
I posted a workaround for this on the Microsoft site, which uses an extra thread to make the call to Invoke method, to avoid the deadlock.
VB.NET example: