错误告诉您窗口的消息队列在向其发布消息时达到了其最大容量。这意味着拥有该窗口的线程不能足够快地处理消息,如果有的话。
我知道这个问题已经有六年了,但我们刚刚遇到了同样的问题,我用它来获取灵感。我不喜欢的是睡眠线程和循环的想法。
相反,我创建了一个WPF窗口。使其透明,高度和宽度为一个像素,并为其添加了Process类型的公共属性。
然后,我写了一个共享子(抱歉,我正在使用VB.NET术语)而不是调用.WaitForExit。
Public Shared Sub DoWaitForProcessToExit(ByVal poProc As Process, ByVal oOwner As Window) Dim oWFE As WaitForProcessToExit poProc.EnableRaisingEvents = True oWFE = New WaitForProcessToExit oWFE.oProc = poProc If Not oOwner Is Nothing Then oWFE.Owner = oOwner End If oWFE.ShowDialog() oWFE = Nothing End Sub
只是 万一发生了疯狂事件,并且该对话框激活时该进程已经退出:
Private Sub WaitForProcessToExit_Activated(sender As Object, e As EventArgs) Handles Me.Activated Try If oProc.HasExited Then Try RemoveHandler oProc.Exited, AddressOf oProc_Exited Catch ex As Exception End Try 'whatever happened .... it seems to have gone too quick for this to invoke the _Exited event Me.Close() End If Catch Try Try RemoveHandler oProc.Exited, AddressOf oProc_Exited Catch ex As Exception End Try Me.Close() Catch End Try End Try End Sub
现在我只需要在加载对话框时执行此操作:
Private Sub WaitForProcessToExit_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded AddHandler oProc.Exited, AddressOf oProc_Exited End Sub
然后只回应退出事件。没有睡觉,没有循环。
Private Sub oProc_Exited(sender As Object, e As EventArgs) 'This event is raised by the exiting process, which is in a different thread, so, invoke my own 'close method from my own Dispatcher Windows.Application.Current.Dispatcher.Invoke(Sub() CloseMe(), Windows.Threading.DispatcherPriority.Render) End Sub Private Sub CloseMe() Me.Close() End Sub