MFC的CDialog多次创建关闭后导致进程无法退出问题

  1. 消息循环判断 WM_QUIT 来退出
  2. CDialog关闭时 在CWnd::OnNcDestroy 时发送WM_QUIT 消息
  3. 发送WM_QUIT 消息时有条件的, 只有当 theApp.m_pMainWnd的 OnNcDestroy触发才会发送
  4. 发送WM_QUIT 消息后, theApp.m_pMainWnd会被清0

所以当第一次窗体销毁后 发送WM_QUIT 消息后, theApp.m_pMainWnd被清0
再次创建关闭后, 因为theApp.m_pMainWnd 是0. 导致无法触发WM_QUIT 消息, 这样消息循环无法退出

	int			i, n;
	n = 16;
	for(i=0; i<n; i++)
	{
		NLog::LogTime(NULL);
		bRetVal = Create(IDD, NULL);
		NLog::LogTime("CDialog.Create");
//		this->SendMessage(WM_CLOSE, 0, 0);
		this->DestroyWindow();
		theApp.m_pMainWnd = this;		//	消息循环 需要m_pMainWnd判断退出, 以发送WM_QUIT
	}
posted @ 2025-04-08 16:11  Yofoo  阅读(13)  评论(0)    收藏  举报