如何在表单关闭时终止 Windows Mobile 6.1 表单应用程序进程?

发布于 2024-09-24 15:27:49 字数 548 浏览 1 评论 0原文

在我编写的 .Net CF 3.5 WinForms 程序的 Program.cs 文件中,我通过实例化 Form1 的新实例来启动该应用程序。

Application.Run(new Form1());

当我想要关闭/退出应用程序时,我会重写 Form1 FormClosed 事件,以便确保退出应用程序。

// In Form1 code-behind
private void OnFormClosed(object sender, EventArgs e)
{
    Application.Exit(); // Doesn't kill process
}

当此代码运行时,我希望应用程序从屏幕上消失(关闭),并且我希望它正在运行的进程消失。该应用程序关闭(消失),但不幸的是,该进程(我们将其称为 app.exe)仍在无限期运行。当我再次启动应用程序并关闭它时,它会启动另一个 app.exe 进程,等等。因此该进程永远不会消失,并且会创建更多进程来消耗内存。

当我关闭/退出应用程序时,如何确保该进程被终止?谢谢。

In the Program.cs file of the .Net CF 3.5 WinForms program I've written, I launch the app by instantiating a new instance of Form1.

Application.Run(new Form1());

When I want to close/exit the application, I override the Form1 FormClosed event so that I can ensure exiting the application.

// In Form1 code-behind
private void OnFormClosed(object sender, EventArgs e)
{
    Application.Exit(); // Doesn't kill process
}

When this code runs, I want the app to disappear from the screen (close) and I want the process it was running as to die. The app closes (disappears), but unfortunately, the process (we'll call it app.exe) is still running indefinitely. When I start the app again and close it, it spins up another app.exe process, etc. So the process is never dying and more are being created eating memory.

How can I ensure this process is being killed when I close/exit the app? Thanks.

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

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

发布评论

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

评论(2

泪之魂 2024-10-01 15:27:49

首先,您不必覆盖 OnFormClosed 即可获得您想要的行为。只需关闭传递给 Application.Run 的表单就会导致应用程序的消息泵退出,并且应该导致进程结束。对于 Windows Mobile,您应该将表单的 ShowMinimize 属性设置为 false 以防止其最小化 - 右上角应该有一个 (OK),而不是 (X)。

如果此表单关闭时进程未退出,则 99.99% 的情况是罪魁祸首是您有一个仍在运行的工作线程,导致进程无法关闭。确保所有线程的 IsBackground 设置为 true(对于 CF 3.5),并且作为预防措施,所有线程还应该具有某种形式的退出通知或标志。不要依赖进程终止来为您拆除它们。

First, you should not have to overide OnFormClosed to get the behavior you're after. Just closing the Form passed in to Application.Run will cause the app's message pump to exit and should cause the process to end. For Windows Mobile you should set the ShowMinimize property of the Form to false to prevent it from just being minimized - you should have a (OK) in the upper right, not an (X).

If the process does not exit when this Form closes, then the culprit 99.99% of the time is that you have a worker thread that is still running preventing the process from closing. Make sure all of your Threads have IsBackground set to true (for CF 3.5) and as a precaution, all of your threads should also have some form of exit notification or flag. Don't rely of the process terminating to tear them down for you.

若相惜即相离 2024-10-01 15:27:49

您的问题是您有 Form.MinimizeBox = true。将其更改为 false,当单击 ok 时,表单将关闭,而不是在 x 时最小化 被点击。

您的问题中还有更多需要考虑的事情:

我重写 Form1 FormClosed 事件

  • System.Windows.Forms.Form 类型的表单没有 FormClosed 事件,只有 Closing已关闭。
  • 您不能覆盖事件。您可以从基类或接口覆盖虚拟函数。

Your problem is that you have Form.MinimizeBox = true. Change it to false and the form will be closed when ok is clicked instead of minimized when x is clicked.

There are more things to consider in your question:

I override the Form1 FormClosed event

  • There is no FormClosed event for forms of type System.Windows.Forms.Form, only Closing and Closed.
  • You don't override an event. You override a virtual function from a base class or interface.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文