启动新流程并终止当前流程
我想从当前执行进程A.exe启动一个新进程B.exe。
一旦B.exe启动,我想杀死A.exe(当前正在执行的进程)。
虽然我可以开始B.exe我无法关闭我当前的过程,即A.exe。
我使用的代码是:
//Start the BT Setup Process ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe"); Process.Start(startInfo); //Terminate the FSA Process[] myProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); foreach (Process process in myProcess) { process.CloseMainWindow(); //all the windows messages has to be processed in the msg queue //hence call to Application DoEvents forces the MSG Application.DoEvents(); }
为什么你想从B
开始关闭A
而A
开始B
然后自己关闭?
Process.Start("A.exe"); Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else
如果您正在尝试启动进程,并在之后终止自己的进程,请使用Environment.Exit(0)
,而不是Application.Exit()
。
尝试Process.Kill()而不是Process.CloseMainWindow() 。
如果您只想关闭当前进程,您应该只能调用Application.Exit而不是循环并关闭进程。
我知道这是旧的,但在.net 4.0中你可以做到
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe"); startInfo.UseShellExecute = true;//This should not block your program Process.Start(startInfo);
然后Application.Exit或我在启动一个只在Block.readline()上阻塞的控制台应用程序后使用close form方法使用winforms应用程序测试的任何内容;