开发者

Start a new process and Killing the current process

I want to start a new process B.exe from the current executing process A.exe.

And as soon as B.exe is launched I want to kill A.exe (the current executing process).

Though I can start B.exe I cannot close my current process i.e A.exe.

Code I use is:

开发者_JAVA百科//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();
}


Why do you want to close A from B while A cat start B and then close by itself?

Process.Start("A.exe");
Process.GetCurrentProcess().Kill(); // or Application.Exit(); or anything else


If you're falling into this quest of starting a process, and kill your own process after, use Environment.Exit(0), not Application.Exit().


Try Process.Kill() instead of Process.CloseMainWindow().


I know this is old but in .net 4.0 you can do

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\TEST\B.exe");
startInfo.UseShellExecute = true;//This should not block your program
Process.Start(startInfo);

Then Application.Exit or whatever I tested with a winforms application using the close form method after launching a console app that just blocks on Console.readline();


If you just want to close the current process you should be able to just call Application.Exit rather than looping through and closing processes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜