是否可以处理事件,关闭流程应用程序

Is it possible to handle event, closing process apps

如何确定我的 process.start 是关闭还是退出?关闭浏览器时是否可能有事件?我正在尝试创建这样的代码: If Process.Exit("iexplore.exe") then, environment.exit(0).

这是我当前的代码,但我的问题是如何确定浏览器是否已关闭?

  Private Sub login(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
       Process.Start("iexplore.exe")
   End sub

您可以将进程分配给一个变量并等待它退出:

 Private Sub login(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
   Dim ieproc As Process
   ieproc = Process.Start("iexplore.exe")
   ieproc.WaitForInputIdle()
   ieproc.WaitForExit() '<= processing will wait here until the browser is closed
   MsgBox("IE has closed!")
 End sub

或者如果您不想停止处理,请不要使用 WaitForExit 而是使用后台进程或计时器来定期检查进程是否已结束...这是一个简单的示例,使用计时器:

 Dim ieproc As Process

 Private Sub login(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
   ieproc = Process.Start("iexplore.exe")
   ieproc.WaitForInputIdle()
   Timer1.Enabled = True
 End sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    If ieproc.HasExited Then
        Me.Timer1.Enabled = False
        MsgBox("IE has closed!")
    End If
End Sub

连接 Process.Exited Event. You must also enable the Process.EnableRaisingEvents 属性:

Private Sub login(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
    Dim P As New Process
    P.StartInfo.FileName = "iexplore.exe"
    P.EnableRaisingEvents = True
    AddHandler P.Exited, AddressOf P_Exited
    P.Start()
End Sub

Private Sub P_Exited(sender As Object, e As EventArgs)
    ' ... do something in here ...
End Sub