if (AfxGetMainWnd() != NULL)
{
AfxGetMainWnd()->SendMessage(WM_CLOSE);
}
//or
::PostQuitMessage(0);
MFC: Closing the Main App From Code
I am attempting to close my MFC application my simulating the user clicking on the "close" icon of the window, or selecting "exit" from the the menu window.
I have searched the web and the answer is to either use this code:
The first option starts to close down my program, but I still have threads running that the OnIdle function of the main app will still call, which results in calls to unallocated memory thus a crash. The second option freezes the program.
However, when I manually select "Exit" from the main MFC window menu, or just close the window by clicking the X. The whole application closes down fine.
I do realize that when I create other windows that are based off of the main MFC window, I can create and override the OnCancel() button and command to do as I wish, however I am not sure if I can do that with the main App window itself.
Does anyone know how to simulate one of those two options in code? Or perhaps understand why other threads are still being called when I use the "AfxGetMainWnd()->SendMessage(WM_CLOSE);" method and have a way to fix it?
Thanks for any help.
Carradine
[Edited by - Carradine on October 3, 2006 4:11:10 PM]
I may be getting closer to the problem.
It seems that when I call my WM_CLOSE command, the IsIdle function is still going to call the function that I call within that function.
Here is my code for IsIdle for the main App:
tWnd->DXDialog->Idling(); Is being called AFTER I call the AfxGetMainWnd()->SendMessage(WM_CLOSE); line. So it goes into that function and accesses memory that is no longer valid.
Again, when I exit the app using the menu, or clicking the red X in the upper right corner, everything closes down fine.
UPDATE:
Well, for now what I do is use a bad hack to fix the problem. When I want to quit the Application, I make a variable = true. I let the OnIdle function run until it finally registers that the variable is actually equal to true for its next run. Once that is known to the thread, my Idling() function is skipped and I can now safely call the ON_CLOSE command without calling code that is in the process of being de-allocated.
[Edited by - Carradine on October 3, 2006 4:56:16 PM]
It seems that when I call my WM_CLOSE command, the IsIdle function is still going to call the function that I call within that function.
Here is my code for IsIdle for the main App:
BOOL CVantEditApp::OnIdle(LONG lCount) { m_pMainWnd; CMainFrame * tWnd = (CMainFrame*)m_pMainWnd; if(tWnd->DXDialog->MainVant->bDone == 0) { if(tWnd->DXDialog->IsWindowVisible()) { tWnd->DXDialog->Idling(); } } // TODO: Add your specialized code here and/or call the base class return CWinApp::OnIdle(lCount);}
tWnd->DXDialog->Idling(); Is being called AFTER I call the AfxGetMainWnd()->SendMessage(WM_CLOSE); line. So it goes into that function and accesses memory that is no longer valid.
Again, when I exit the app using the menu, or clicking the red X in the upper right corner, everything closes down fine.
UPDATE:
Well, for now what I do is use a bad hack to fix the problem. When I want to quit the Application, I make a variable = true. I let the OnIdle function run until it finally registers that the variable is actually equal to true for its next run. Once that is known to the thread, my Idling() function is skipped and I can now safely call the ON_CLOSE command without calling code that is in the process of being de-allocated.
[Edited by - Carradine on October 3, 2006 4:56:16 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement