启动新流程并终止当前流程

sri*_*tsa 16 c# process

我想从当前执行进程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();
}
Run Code Online (Sandbox Code Playgroud)

aba*_*hev 8

你为什么要关闭AB,而A猫开始B,然后自行关闭?

Process.Start("A.exe");
Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else
Run Code Online (Sandbox Code Playgroud)


小智 6

如果你正在进入这个开始一个过程的任务,并在之后杀死你自己的过程,请使用Environment.Exit(0),而不是Application.Exit().

  • 如果有任何活动的前台线程,应用程序将不会退出;Process.GetCurrentProcess().Kill(); 似乎更具确定性。 (2认同)