如何退出WIN32应用程序 [英] How to exit a WIN32 app

查看:514
本文介绍了如何退出WIN32应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CWnd窗口(而不是CFrameWnd)的OnTimer()中退出MFC应用程序。当我调用PostQuitMessage(0)时,应用程序退出OK但有很多未销毁的对象,包括我的CDocument对象。如果我退出使用文件/退出,没有内存泄漏。



任何建议我可能做错了什么?有没有更好的方法可以干净地退出?



谢谢



Tony

I am trying to exit an MFC app from within the OnTimer() of a CWnd window (not the CFrameWnd). When I call PostQuitMessage(0), the app exits OK but with lots of undestroyed objects, including my CDocument object. If I exit using File/Exit, there are no memory leaks.

Any suggestions what I might be doing wrong? Is there a better way to cleanly exit?

Thanks

Tony

推荐答案

在MFC中,当主窗口被破坏时,应用程序退出。只需模拟主窗口关闭。在主窗口发布WM_CLOSE消息。这甚至可以从另一个过程中运行当然这样主窗口有机会处理WM_CLOSE并取消主窗口关闭(例如弹出你想保存文件吗?msgbox)但这很自然。你不应该突然终止进程。
In MFC the app exits when the main window gets destroyed. Just simulate main window close. Post the main window a WM_CLOSE message. This works even from another process. Of course this way the main window has the chance to handle WM_CLOSE and to cancel the main window close (for example to popup a "do you want to save the document?" msgbox) but that is natural. You aren't supposed to abruptly terminate a process.


softwaremonkey写道:
softwaremonkey wrote:

如果我的客户端应用程序没有在10秒钟内没有得到服务器的回复,然后出现问题,我想退出而不是保持开放,什么都不做。

If my client application does not get a reply from the server within 10 seconds then something is wrong and I would like to exit rather than just stay open and doing nothing.

我不认为这样的功能会有任何益处。也许,你没有彻底考虑过这种情况。



你的应用程序应该真的无所事事,直到用户参与其中。它应该足够详细地显示错误条件并等待用户。想想你的建议:它不会为用户提供必要的反馈。在未经用户同意的情况下消失应用程序通常是完全令人困惑的,绝不应该这样做。



-SA

I don't think such feature would be beneficial in any way. Probably, you did not think on such situations thoroughly.

Your application should really stay doing nothing, until the user attends to it. It should show an error condition in sufficient detail and wait for the user. Just think about what you suggested: it won't provide the user with essential feedback. Disappearing of the application without user's consent is generally totally confusing, should never be done.

—SA


只需从WinMain函数返回。如果您有消息循环,请使用PostQuitMessage进行分组。不要试图在执行过程中退出进程;这很草率。
Just return from the WinMain function. If you have a message loop, use PostQuitMessage to break out. Don't try to exit the process in the middle of execution; it's sloppy.


这篇关于如何退出WIN32应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆