进程退出的问题

发布于 2024-10-07 16:44:15 字数 327 浏览 0 评论 0 原文

假设我有一个 ID 为 1234 的进程。该进程在我的应用程序运行之前运行。

我有这样的代码:

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

现在,当我编译并运行应用程序时,它会获取进程并显示主窗口标题。但是,当我关闭进程 1234 时,app.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-10-14 16:44:15

请注意,文档指出EnableRaisingEvents 必须设置为 true 才能触发此事件。

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

┊风居住的梦幻卍 2024-10-14 16:44:15

默认情况下,出于性能原因,Process 类不会引发事件。如果您希望 Process 对象监视 Exited 并引发该事件,则需要设置其 EnableRaisingEvents 属性设置为 true。

存在相关成本
监视进程退出。如果
EnableRaisingEvents 为 true 时,
当退出事件被引发时
关联进程终止。这
您指定的程序
Exited 事件在那时运行。

有时,您的应用程序会启动
过程,但不需要
已通知其关闭。例如,
您的应用程序可以启动记事本
允许用户执行文本操作
编辑,但不再使用
记事本应用程序。你可以
选择不收到通知时
进程退出,因为它不是
与持续经营有关
您的申请。环境
EnableRaisingEvents 错误保存
系统资源。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文