作者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