作者deerdriver (bun)
看板C_Sharp
標題[問題] 關閉 BackgroundWorker 中的 process
時間Tue Feb 17 18:20:37 2009
我執行了一個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
1F:→ deerdriver:我用的是VC2008 02/17 18:21
2F:推 toki:WaitForExit執行後必需等外部Process結束後才return 02/17 19:37
3F:推 toki:另外可以試一下 Process.Kill() 來強制終結 process 02/17 19:42
4F:→ toki:中間插一行「所以 thread 會沒有反應」 02/17 19:42