Jaunedeau
Jaunedeau

Reputation: 31

How to let a C# application finish after if has started a new process?

I have an application which generates a .pdf file and open it using myProcess.start(pdfFileName); . This called is called in the event handler of a click event on a button in a Winfom app.

Everything works fine, except that if I quit (alt-f4 or using the top-right cross) my application after Acrobat Reader have been started, my application do not stop : the Form disapears, but the debugging session in VS do not stop, even if I have already quit Acrobat Reader. The beahaviour is the same if I compile in realese and/or start the exe from windows and not from VS, I then have to kill the process with the task manager.

I could find nothing in the documentation, but I understand this must be a very common problem ?

Thanks, Jaune.

Upvotes: 3

Views: 384

Answers (4)

explorer
explorer

Reputation: 12090

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

Upvotes: 0

Ruel
Ruel

Reputation: 15780

I just tested it, with Acrobat reader as well, and the application exits normally. You must have left a background process or thread(s) running in a loop.

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

In the section of your code that runs when the app is shutdown, for example an on exit event you could kill the process. See http://msdn.microsoft.com/en-us/library/05abh773(v=vs.71).aspx

Upvotes: 0

Preet Sangha
Preet Sangha

Reputation: 65496

You need to hook 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();
}

Upvotes: 2

Related Questions