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

时间:2015-01-12 02:49:57

标签: vb.net

如何确定我的process.start是关闭还是退出?关闭浏览器时是否有可能发生事件?我正在尝试创建这样的代码:如果是Process.Exit(“iexplore.exe”)那么,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

2 个答案:

答案 0 :(得分:0)

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

 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

答案 1 :(得分:0)

连接Process.Exited事件。您还必须启用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