Sign in to follow this  

MFC: Closing the Main App From Code

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
if (AfxGetMainWnd() != NULL)
{
	AfxGetMainWnd()->SendMessage(WM_CLOSE);
}
//or 
::PostQuitMessage(0);

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]

Share this post


Link to post
Share on other sites
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:




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]

Share this post


Link to post
Share on other sites

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this