vb.net检查过程结束,修改UI

vb.net checking process is ended, and modify the UI

我有一个问题,我的程序(启动进程时)最小化到系统托盘中,当进程结束时,它显示为正常状态,但它不适用于我的解决方案,我收到此错误消息:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

我的代码:

Dim p as new Process
p.filename = "somefile.exe"
p.workingdirectory = "somepath"
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf Main.Main_Process_Closed
p.Start()

主窗体中的处理程序:

Public Sub Main_Process_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Visible = True
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
    End Sub

但问题是什么?感谢您的帮助。

添加处理程序时,处理程序可能会被 UI 线程以外的辅助线程调用。因此,您无法直接操作 UI 线程。

您将需要使用 Invoke 正确编组到 UI 线程。这是一个很棒的 MSDN article 应该可以引导您完成它。