Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Firstly, I'm noobish to MFC, but I have googled and searched CP extensively for the answer to my problem (including Newcomer's excellent articles), but have had no joy. I'd really appreciate any help you can offer.

I have an SDI app, with Doc/View architecture. The CDocument class contains initialization data that governs a numerically intensive simulation object, contained in CDocument. The CView class interacts with the user to set the initialization data.

To keep the UI active (but with commands disabled as appropriate), I have started a thread to run the simulation from CView. All is fine when the user behaves appropriately and waits for the thread to end and then terminate the application. I want to handle a bad user though! I want to handle the user that tries to terminate the application during the worker thread execution. Following Newcomer's article I have a bool flag variable in CView that is set to false when the thread is to be stopped.

Here is the problem: The CView OnClose() event is called after the CDocument::OnCloseAllDocuments() crashing the simulation as its initialization data pointers are freed. I can't get a pointer to my CView object in the CDocument class, as the program won't compile when I include MyView.h in MyDoc.h. I've seen suggestions of hosting the thread from CMainFrame (which is first in the application termination sequence), but can't put the thread there as it's instantiated from CView.

The worst thing is that I know this is surely a quite standard design scenario, but one that I cannot crack :( I'd appreciate any help!
Posted

1 solution

Two possible solutions here:

Override the WM_CLOSE message:

1. And don't allow the closing to continue until the thread has finished.

2. Process the interruption and then exit as always.

take a look at this link: Windows Message Handling - Part 1[^]

It seems that somebody has a similar problem here: Window is not closing by clicking close icon in dialog in VC++[^]

HTH...
 
Share this answer
 
v2
Comments
cc.caprani 13-Apr-11 11:30am    
Joan,
Thanks for replying. Can I be bold and ask two follow up questions to your solutions?!
1. Where should I handle the WM_CLOSE message so that it doesn't get called after the CDocument is already destroyed. I tried handling it from the CView class and it didn't work. If I try to handle it from CMainFrame then I need to access CView (where the threads lives) but I can't include MyView.h in MainFrame.h - tried it!
2. I'd like to process the interruption, but this is the essence of my problem - how do I do this? OnClose() in CView is too far down the terminating chain.
Thanks again for your help!
Joan M 13-Apr-11 11:41am    
Look for the toppest element (CMainFrame should be if I remember well). Handle the WM_CLOSE there, then you should get access to a public getter function in your CView that should give you the state of the thread. Once you have that then you can proceed to handle it to fit your needs...

I can't remember it properly... but if handling WM_CLOSE its not enough as it becomes called after your needs, then use PreTranslateMessage to capture and cancel it...
cc.caprani 13-Apr-11 12:09pm    
I tried this already, but maybe I did it wrong:

void CMainFrame::OnClose()
{
CMyView* pView = (CMyView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
ASSERT_VALID(pView);
pView->OnClose();
CFrameWnd::OnClose();
}

I include MyView.h in the top of the MainFrame.cpp file. It won't compile then though :(

Thanks again for your help!
cc.caprani 13-Apr-11 12:21pm    
It didn't compile because the MyDoc.h wasn't included in the MyView.h file. I wasn't sure if cross-including them would lead to melt down so I hadn't before this. After including the View.h in MainFrame.cpp and MyDoc.h in MyView.h it compiles and the above code works. I can now handle the closing of the thread locally to the CView class. Of course I have other problems now....!!

Thanks for your help in solving this Joan!
Joan M 13-Apr-11 12:31pm    
Been on the way home, anyway happy to see it is working now... ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900