我已经看到了关于control.Invoke悬挂应用的问题一吨后,但他们大多似乎仅限于那些在.NET 1.1上运行。 我也见过建议使用.BeginInvoke,而不是我所尝试,但都无济于事。 .BeginInvoke不挂,但它也不会调用委托。 我主要担心的是,我已经使用.Invoke多年,没有问题,我已经在使用它广泛领域的重大应用程序,我很担心这个问题会突然出现在那里。 我什么都不做不同的,据我可以在我的工作代码和失败之间的代码告诉。 我已经写了代码死的简单一点的是复制问题(都在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
如果任何人都可以看到我做了什么错在这里我会很感激!