如何在表单关闭时终止 Windows Mobile 6.1 表单应用程序进程?
在我编写的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不必覆盖 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 totrue
(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.您的问题是您有
Form.MinimizeBox = true
。将其更改为false
,当单击 ok 时,表单将关闭,而不是在 x 时最小化 被点击。您的问题中还有更多需要考虑的事情:
System.Windows.Forms.Form
类型的表单没有FormClosed
事件,只有Closing
和已关闭。
Your problem is that you have
Form.MinimizeBox = true
. Change it tofalse
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:
FormClosed
event for forms of typeSystem.Windows.Forms.Form
, onlyClosing
andClosed
.