2015-01-12 143 views
0

如何確定我的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 

回答

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 
+0

的邏輯是好了,但是我還沒有收我的互聯網瀏覽器,它已經顯示出我的消息「IE已經關閉」。我想要的只是當我關閉瀏覽器時,提示信息將顯示。 – PACMAN

+0

使用WaitForExit或Timer的方法?我只測試了兩個,他們的工作。瀏覽器關閉前沒有消息框。 –

+0

嘗試添加'ieproc.WaitForInputIdle()'...看到我上面更新的代碼。 –

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 
相關問題