How to allow C # application termination after starting a new process?

I have an application that generates a .pdf file and opens it using myProcess.start (pdfFileName) ;, This call is called in the click event event handler on a button in a Winfom application.

Everything works fine, except that if I leave (alt-f4 or using the upper right cross), my application does not stop after running Acrobat Reader: the form disappears, but the debugging session in VS does not stop, even if I already left Acrobat Reader. Beahaviour is the same if I compile in realese and / or run exe from windows, not from VS, then I need to kill the process using task manager.

I could not find anything in the documentation, but I understand that this should be a very common problem?

Thanks, Jon.

+3
source share
4 answers

You need to hook up the ProcessExit event

myProcess.StartInfo.FileName = fileName;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();

[...]

private void myProcess_Exited(object sender, System.EventArgs e) {
  Application.Exit();
}
+2
source

In the section of your code that runs when the application terminates, for example, in the event of a completion event, you can kill the process. See http://msdn.microsoft.com/en-us/library/05abh773(v=vs.71).aspx

0
source

, Acrobat reader, . , .

0
theProcess.EnableRaisingEvents = true;
theProcess.Exited +=  delegate(object sender, System.EventArgs e)
                                    { Application.Exit();  }
theProcess.Start();
0

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


All Articles