Exiting the parent process when the child process is running

I have a WinForms application. And here is the code from the Form_Load method.

Process proc = new Process(); proc.StartInfo.FileName = @"C:\program files\MyProgram\start.exe"; proc.StartInfo.Arguments = Application.ExecutablePath; proc.Start(); Application.Exit(); 

Why does my parent process not close the program when I call Application.Exit? How to identify a problem?

+4
source share
2 answers

Once you start a new process, its service life is independent of your current applications. If you want the execution artifact directly tied to the lifetime of the current application, use threads.

If you want to create a child process and then complete it, you need to kill it:

 Process proc = new Process(); proc.StartInfo.FileName = @"C:\program files\MyProgram\start.exe"; proc.StartInfo.Arguments = Application.ExecutablePath; proc.Start(); // When terminating: proc.Kill(); proc.WaitForExit(); Application.Exit(); 

However, I would not recommend the above for most scenarios, as this can lead to data corruption if the child process is killed during a critical operation (for example, saving to a file).

+3
source

To exit the Windows Forms application, you must close the main form. Look at the code in Program.Main (), and you will see that the main thing that supports the main thread is the main form. Close it and the application will exit.

0
source

Source: https://habr.com/ru/post/1391099/


All Articles