[C#] 等待啟動的進程執行完畢


有能有時候我們啟動了一個進程,必須等到此進程執行完畢,或是,一段時間,

關閉進程后再繼續往下走。

 

Example

sample1

等待應用程序執行完畢

//等待應用程序執行完畢
        private void btnProcessIndefinitely_Click(object sender, EventArgs e) {
            //配置文件案路徑
            string target = System.IO.Path.Combine(Application.StartupPath,@"Test.txt");
            //取得完整絕對路徑
            target = System.IO.Path.GetFullPath(target);
            //啟動進程
            Process p = Process.Start(target);
            //讓 Process 組件等候相關的進程進入閑置狀態。 
            p.WaitForInputIdle();
            //設定要等待相關的進程結束的時間,並且阻止目前的線程執行,直到等候時間耗盡或者進程已經結束為止。 
            p.WaitForExit();

            if (p != null) {
                p.Close();
                p.Dispose();
                p = null;
            }

            this.Close();

        }

 

sample2

等待應用程序(7秒)

//等待應用程序(7秒)
        private void btnWaitProcessfor7_Click(object sender, EventArgs e) {
            //配置文件案路徑
            string target = System.IO.Path.Combine(Application.StartupPath, @"Test.txt");
            //取得完整絕對路徑
            target = System.IO.Path.GetFullPath(target);
            //啟動進程
            Process p = Process.Start(target);
            //讓 Process 組件等候相關的進程進入閑置狀態。 
            p.WaitForInputIdle();
            //設定要等待相關的進程結束的時間,這邊設定 7 秒。 
            p.WaitForExit(7000);

            //若應用程序在指定時間內關閉,則 value.HasExited 為 true 。
            //若是等到指定時間到了都還沒有關閉程序,此時 value.HasExited 為 false,則進入判斷式
            if (!p.HasExited) {

                //測試進程是否還有響應
                if (p.Responding) {

                    //關閉用戶接口的進程
                    p.CloseMainWindow();

                } else {
                    //立即停止相關進程。意即,進程沒回應,強制關閉
                    p.Kill();
                }
            }

            if (p != null) {
                p.Close();
                p.Dispose();
                p = null;
            }

            this.Close();
        }

 

sample3

使用多線程等候應用程序(7秒)

以上兩種方法,在等待進程完成時,窗體畫面會 lock 住,無法重繪,這邊提供一個改善的方法,

若有其他方法,望前輩指導。

 

//使用多線程等候應用程序(7秒)
        private void btnMultiThreadWaitProcess_Click(object sender, EventArgs e) {
            //建立線程對象
            Thread thread = new Thread(new ThreadStart(StartProcess));

            //啟動線程
            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");
            //取得完整絕對路徑
            target = System.IO.Path.GetFullPath(target);
            //啟動進程
            Process p = Process.Start(target);

            //讓 Process 組件等候相關的進程進入閑置狀態。 
            p.WaitForInputIdle();
            //設定要等待相關的進程結束的時間,這邊設定 7 秒。 
            p.WaitForExit(7000);

            //若應用程序在指定時間內關閉,則 value.HasExited 為 true 。
            //若是等到指定時間到了都還沒有關閉程序,此時 value.HasExited 為 false,則進入判斷式
            if (!p.HasExited) {

                //測試進程是否還有響應
                if (p.Responding) {

                    //關閉用戶接口的進程
                    p.CloseMainWindow();

                } else {
                    //立即停止相關進程。意即,進程沒回應,強制關閉
                    p.Kill();
                }
            }

            if (p != null) {
                p.Close();
                p.Dispose();
                p = null;
            }
        }     

源代碼

TestProcessWaitting.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM