MFC Doc/View架构和线程 [英] MFC Doc/View Architecture and Threads

查看:74
本文介绍了MFC Doc/View架构和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对MFC不太满意,但我已经在CP上进行了广泛的Google搜索和搜索,以找到解决我的问题的答案(包括Newcomer的出色文章),但并不高兴.非常感谢您能提供的任何帮助.

我有一个具有Doc/View体系结构的SDI应用程序. CDocument类包含初始化数据,该初始化数据控制CDocument中包含的数字密集型模拟对象. CView类与用户交互以设置初始化数据.

为了保持UI处于活动状态(但在适当情况下禁用了命令),我启动了一个线程来从CView运行模拟.当用户行为适当并等待线程结束然后终止应用程序时,一切都很好.我想处理坏用户!我想处理在工作线程执行期间尝试终止应用程序的用户.在Newcomer的文章之后,我在CView中有一个bool标志变量,当线程要停止时,该变量设置为false.

这是问题所在:由于释放了初始化数据指针,CDocument :: OnCloseAllDocuments()在模拟崩溃后调用了CView OnClose()事件.我无法在CDocument类中获得指向我的CView对象的指针,因为当我在MyDoc.h中包含MyView.h时,该程序将无法编译.我已经看到了从CMainFrame托管线程的建议(这是应用程序终止序列中的第一个),但是无法将线程放在那里,因为它是从CView实例化的.

最糟糕的是,我知道这肯定是一个相当标准的设计方案,但是我无法破解它:(我将不胜感激!

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!

推荐答案

两个可能的解决方案在这里:

覆盖WM_CLOSE消息:

1.并且在线程完成之前,不允许关闭继续进行.

2.处理中断,然后照常退出.

看一下此链接: Windows消息处理-第1部分 [在VC ++中单击对话框中的关闭图标,无法关闭窗口 [
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...


这篇关于MFC Doc/View架构和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆