Issue with Process Exited

好久不见. 提交于 2020-01-02 02:11:38

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4457252/issue-with-process-exited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!