[問題] 關閉 BackgroundWorker 中的 process

看板C_Sharp作者 (bun)時間17年前 (2009/02/17 18:20), 編輯推噓2(202)
留言4則, 2人參與, 最新討論串1/1
我執行了一個BackgroundWorker去執行ExecuteAvid這個method 在這個method中 new process 去執行外部的exe檔 如果我想要在程式中 當exe檔執行還沒結束的時候 去中斷這個外部的exe檔的執行該怎麼做比較好 我找不到BackgroundWorker所在的thread 所以就算執行myprocess.close也是沒有用 嘗試用BackgroundWorker的disposed event去觸發 不過也是沒有辦法找到BackgroundWorker執行的thread 請問該怎樣才能關掉外部的exe檔 private BackgroundWorker back; back = new BackgroundWorker(); back.DoWork += new DoWorkEventHandler(ExecuteAvid); back.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CloseWindow); back.RunWorkerAsync(); void ExecuteAvid(object sender, DoWorkEventArgs e) { Debug.WriteLine("back thread maybe"); Debug.WriteLine(String.Format("Thread ID = {0}", Thread.CurrentThread.ManagedThreadId)); myprocess = new Process(); try { myprocess.StartInfo.CreateNoWindow = true; myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; myprocess.StartInfo.Arguments = arg; myprocess.StartInfo.FileName = folderPath + "//writeavidmxf.exe"; myprocess.Start(); myprocess.WaitForExit(); } catch (Win32Exception e32) { Debug.WriteLine("win32Exception"+e32); exe.Dispose(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.211.242.19

02/17 18:21, , 1F
我用的是VC2008
02/17 18:21, 1F

02/17 19:37, , 2F
WaitForExit執行後必需等外部Process結束後才return
02/17 19:37, 2F

02/17 19:42, , 3F
另外可以試一下 Process.Kill() 來強制終結 process
02/17 19:42, 3F

02/17 19:42, , 4F
中間插一行「所以 thread 會沒有反應」
02/17 19:42, 4F
文章代碼(AID): #19cezsLS (C_Sharp)