Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Process Exited

lets say I have a process with the ID 1234. This process is running before my application runs.

I have this code:

        Process app = Process.GetProcessById(1234);
        MessageBox.Show(app.MainWindowTitle);
        app.Exited += this.methodShowsMessageBox;

Now, when I compile and run the app, it gets the process and shows the main window title. However when i close process 1234, the app.Exited does nto fire... why is this? And how would i get it to fire?

like image 580
Ozzy Avatar asked Dec 16 '10 03:12

Ozzy


2 Answers

Please note that the documentation states that EnableRaisingEvents must be set to true before this event will fire.

like image 127
Dark Falcon Avatar answered Sep 20 '22 19:09

Dark Falcon


By default, for performance reasons, the Process class does not raise events. If you want the Process object to watch for Exited and raise that event, you need to set its EnableRaisingEvents property to true.

There is a cost associated with watching for a process to exit. If EnableRaisingEvents is true, the Exited event is raised when the associated process terminates. The procedures that you have specified for the Exited event run at that time.

Sometimes, your application starts a process but does not need to be notified of its closure. For example, your application can start Notepad to allow the user to perform text editing, but make no further use of the Notepad application. You can choose to not be notified when the process exits, because it is not relevant to the continued operation of your application. Setting EnableRaisingEvents to false saves system resources.

like image 38
Josh Avatar answered Sep 20 '22 19:09

Josh