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!