[C#] 等待启动的进程执行完毕
生活随笔
收集整理的這篇文章主要介紹了
[C#] 等待启动的进程执行完毕
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有能有時(shí)候我們啟動(dòng)了一個(gè)進(jìn)程,必須等到此進(jìn)程執(zhí)行完畢,或是,一段時(shí)間,
關(guān)閉進(jìn)程后再繼續(xù)往下走。
?
Example
sample1
等待應(yīng)用程序執(zhí)行完畢
//等待應(yīng)用程序執(zhí)行完畢private void btnProcessIndefinitely_Click(object sender, EventArgs e) {//配置文件案路徑string target = System.IO.Path.Combine(Application.StartupPath,@"Test.txt");//取得完整絕對(duì)路徑target = System.IO.Path.GetFullPath(target);//啟動(dòng)進(jìn)程Process p = Process.Start(target);//讓 Process 組件等候相關(guān)的進(jìn)程進(jìn)入閑置狀態(tài)。 p.WaitForInputIdle();//設(shè)定要等待相關(guān)的進(jìn)程結(jié)束的時(shí)間,并且阻止目前的線程執(zhí)行,直到等候時(shí)間耗盡或者進(jìn)程已經(jīng)結(jié)束為止。 p.WaitForExit();if (p != null) {p.Close();p.Dispose();p = null;}this.Close();}?
sample2
等待應(yīng)用程序(7秒)
//等待應(yīng)用程序(7秒)private void btnWaitProcessfor7_Click(object sender, EventArgs e) {//配置文件案路徑string target = System.IO.Path.Combine(Application.StartupPath, @"Test.txt");//取得完整絕對(duì)路徑target = System.IO.Path.GetFullPath(target);//啟動(dòng)進(jìn)程Process p = Process.Start(target);//讓 Process 組件等候相關(guān)的進(jìn)程進(jìn)入閑置狀態(tài)。 p.WaitForInputIdle();//設(shè)定要等待相關(guān)的進(jìn)程結(jié)束的時(shí)間,這邊設(shè)定 7 秒。 p.WaitForExit(7000);//若應(yīng)用程序在指定時(shí)間內(nèi)關(guān)閉,則 value.HasExited 為 true 。//若是等到指定時(shí)間到了都還沒有關(guān)閉程序,此時(shí) value.HasExited 為 false,則進(jìn)入判斷式if (!p.HasExited) {//測(cè)試進(jìn)程是否還有響應(yīng)if (p.Responding) {//關(guān)閉用戶接口的進(jìn)程p.CloseMainWindow();} else {//立即停止相關(guān)進(jìn)程。意即,進(jìn)程沒回應(yīng),強(qiáng)制關(guān)閉p.Kill();}}if (p != null) {p.Close();p.Dispose();p = null;}this.Close();}?
sample3
使用多線程等候應(yīng)用程序(7秒)
以上兩種方法,在等待進(jìn)程完成時(shí),窗體畫面會(huì) lock 住,無法重繪,這邊提供一個(gè)改善的方法,
若有其他方法,望前輩指導(dǎo)。
?
//使用多線程等候應(yīng)用程序(7秒)private void btnMultiThreadWaitProcess_Click(object sender, EventArgs e) {//建立線程對(duì)象Thread thread = new Thread(new ThreadStart(StartProcess));//啟動(dòng)線程thread.Start();//等待線程處理完畢while (thread.ThreadState == System.Threading.ThreadState.Running ||thread.ThreadState == System.Threading.ThreadState.WaitSleepJoin) {Application.DoEvents();}this.Close(); }private void StartProcess() {//配置文件案路徑string target = System.IO.Path.Combine(Application.StartupPath, @"Test.txt");//取得完整絕對(duì)路徑target = System.IO.Path.GetFullPath(target);//啟動(dòng)進(jìn)程Process p = Process.Start(target);//讓 Process 組件等候相關(guān)的進(jìn)程進(jìn)入閑置狀態(tài)。 p.WaitForInputIdle();//設(shè)定要等待相關(guān)的進(jìn)程結(jié)束的時(shí)間,這邊設(shè)定 7 秒。 p.WaitForExit(7000);//若應(yīng)用程序在指定時(shí)間內(nèi)關(guān)閉,則 value.HasExited 為 true 。//若是等到指定時(shí)間到了都還沒有關(guān)閉程序,此時(shí) value.HasExited 為 false,則進(jìn)入判斷式if (!p.HasExited) {//測(cè)試進(jìn)程是否還有響應(yīng)if (p.Responding) {//關(guān)閉用戶接口的進(jìn)程p.CloseMainWindow();} else {//立即停止相關(guān)進(jìn)程。意即,進(jìn)程沒回應(yīng),強(qiáng)制關(guān)閉p.Kill();}}if (p != null) {p.Close();p.Dispose();p = null;}}源代碼
TestProcessWaitting.rar
轉(zhuǎn)載于:https://www.cnblogs.com/qqhfeng/p/4769524.html
總結(jié)
以上是生活随笔為你收集整理的[C#] 等待启动的进程执行完毕的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下git的简单运用
- 下一篇: c# 取 list前100条数据