Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a new process and Killing the current process

Tags:

c#

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:

//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();
}
like image 460
srivatsa Avatar asked Nov 25 '10 16:11

srivatsa


People also ask

What is process start?

Start(ProcessStartInfo) Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

How do you terminate a process in C#?

Kill() Immediately stops the associated process.


2 Answers

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
like image 76
abatishchev Avatar answered Oct 10 '22 04:10

abatishchev


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

like image 7
Fabiano Cores Avatar answered Oct 10 '22 04:10

Fabiano Cores