上次我已介紹如何運行外部程序,今天我繼續(xù)講這一話題: 1.有好多時,我們需要調(diào)用外部的EXE程序,并且要等它運行完畢,我們才可以繼續(xù)下面的動作,那我們怎樣去實現(xiàn)了,請看以下代碼. '怎樣等待外部程序運行完畢. '從系統(tǒng)資料夾讀入文件 Dim sysFolder As String = _ Environment.GetFolderPath(Environment.SpecialFolder.System) '創(chuàng)建一個新的進程結(jié)構(gòu) Dim pInfo As New ProcessStartInfo() '設(shè)置其成員FileName為系統(tǒng)資料的Eula.txt pInfo.FileName = sysFolder & "\eula.txt" '運行該文件 Dim p As Process = Process.Start(pInfo) '等待程序裝載完成 p.WaitForInputIdle() '等待進行程退出 p.WaitForExit() '繼續(xù)執(zhí)行下面的代碼 MessageBox.Show("繼續(xù)執(zhí)行代碼")
2.我們想在5秒鐘后,強行關(guān)閉它.而不是需要我手工關(guān)閉. '設(shè)置退出時間 Dim timeOut As Integer = 5000 Dim sysFolder As String = _ Environment.GetFolderPath(Environment.SpecialFolder.System) Dim pInfo As New ProcessStartInfo() pInfo.FileName = sysFolder & "\eula.txt" Dim p As Process = Process.Start(pInfo) p.WaitForInputIdle() p.WaitForExit(timeOut) '檢查是否在超時前已關(guān)閉了. If p.HasExited = False Then '進行程還在運行 '看進程有沒有回應(yīng) If p.Responding Then p.CloseMainWindow() '關(guān)閉窗口 Else p.Kill()'強行中斷 End If End If MessageBox.Show("繼續(xù)執(zhí)行代碼")
|