I've seen a ton of post regarding the problem of control.Invoke hanging applications but they mostly seemed to be restricted to those running on .NET 1.1. I've also seen suggestions to use .BeginInvoke instead which I have tried but to no avail. .BeginInvoke doesn't hang but it also doesn't call the delegate. My main concern is that I have used .Invoke for years with no issues and I have a major application in the field that uses it extensively and I'm worried that this problem will crop up there. I am doing nothing differently as far as I can tell between my working code and the code that fails. I've written up a dead simple bit of code that replicates the issue (all in 4.0 VS2010):
Public Class Form1
Private WithEvents sat As TestInvoke
Private Delegate Sub doTheUpdateDelegate(ByVal message As String)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
sat = New TestInvoke
sat.startAThread()
End Sub
Public Class TestInvoke
Public Event UpdateControl()
Public Sub startAThread()
Dim t As New Threading.Thread(AddressOf _startAThread)
Dim trace As String
trace = "a"
t.SetApartmentState(Threading.ApartmentState.STA)
t.Start()
t.Join()
End Sub
Protected Sub _startAThread()
Try
For k = 0 To 10
System.Threading.Thread.Sleep(1000)
k += 1
RaiseEvent UpdateControl()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Private Sub sat_UpdateControl() Handles sat.UpdateControl
Try
Call doTheupdate(Now.ToString)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub doTheUpdate(ByVal message As String)
Try
If Button1.InvokeRequired = True Then
Dim objParams As Object() = {message}
'hangs on the following line
Button1.Invoke(New doTheUpdateDelegate(AddressOf doTheUpdate), objParams)
Else
Button1.Text = message
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
If anyone can see what I've done wrong here I'd really appreciate it!