`
lovecontry
  • 浏览: 1033733 次
文章分类
社区版块
存档分类
最新评论

MFC应用程序中的多线程与UI窗口

 
阅读更多

参见:http://hi.baidu.com/winnyang/blog/item/e0925616ba82561e962b43c6.html

SUMMARY
In a multi-threaded application written using MFC, you should not pass MFC objects across thread boundaries. As a general rule, a thread should access only those MFC objects that it creates. Failure to do so may cause run-time problems including assertions or unexpected program behavior.
使用MFC编写多线程应用程序,不能跨线程传递MFC对象。作为一种基本的准则,线程只允许操作由其本身创建的MFC对象。不遵守该准则将导致断言(assertion)或者无法预知的程序行为等运行期错误。


MORE INFORMATION
In a Win32 process, all the threads running in the process address space can view all global and static data. A thread can use thread-local-storage (TLS) to store any thread-specific data.
所有运行在win32进程地址空间中的线程能够查看全局和静态的数据。线程使用“线程局部存储(TLS)”技术来存储跟线程相关的数据。


In a multi-threaded environment because windows are owned by threads, MFC keeps the temporary and permanent window handle map in thread local storage. The same is true for other handle maps like those for GDI objects and device contexts. Keeping the window handle maps in thread local storage ensures protection against simultaneous access by several threads.
在多线程环境中,由于所有的窗口元素都通过线程来管理,于是MFC将暂时/永久窗口<->句柄映射保存在TLS中。其他的窗口句柄映射以及设备描述表也是采用类似的方法存储。将这些窗口句柄映射存储在TLS中能够防止其他线程同时访问这些数据。


The behavior of the functions CHandleMap::LookupPermanent() and CHandleMap::LookupTemporary() is a direct consequence of these facts. Given a window handle, these functions check the permanent and temporary window handle maps of the current thread for the existence of an associated CWnd derived MFC object. This means that if calls to these functions are made from a thread to search for MFC objects that represent windows created in other threads, these calls will fail.
函数:CHandleMap::LookupPermanent() 和 CHandleMap::LookupTemporary()就是这种理论的直接产物。给定窗口句柄,这些函数通过查找临时以及永久的窗口句柄映射来获得CWnd或者其子类对象。这就意味着如果一个线程调用这些函数来查找其他线程的CWnd或者其子类对象,调用将失败。


There are several functions that call CHandleMap::LookupPermanent() and CHandleMap::LookupTemporary(). CWnd::AssertValid() (and hence the macro ASSERT_VALID for a CWnd object) is one such function. This function is called to make validity checks on an object. If AssertValid() fails to find an entry for the MFC object's m_hWnd member in any of the handle maps or finds an incorrect entry, it fires an assertion. In Visual C++ 2.1, these assertions are in file Wincore.cpp, lines 797 and 798. In Visual C++ 2.2, they are in Wincore.cpp, lines 804 and 805. In Visual C++ 4.0, they are in Wincore.cpp, lines 871 and 872.
MFC中有大量函数调用CHandleMap::LookupPermanent() and CHandleMap::LookupTemporary()。CWnd::AssertValid()就是其中一个(宏ASSERT_VALID也是一个).调用该函数来检查对象的有效性。如果没有发现有效的CWnd对象,则会报“断言(Assertion)”错误。VC2.1中,该错误出现在wincore.cpp, lines 797 & 798, VC2.2中,出现在wincore.cpp行804和805,VC4.0则出现在行871和872。


Calls to the ASSERT_VALID macro are sprinkled all over the MFC source code. Hence, from a particular thread, if you end up calling a function that calls ASSERT_VALID on MFC window objects that belong to some other thread, you get an assertion. If you do not get an assertion, you may still get abnormal behavior because you are not allowed to directly manipulate windows created by other threads.
宏ASSERT_VALID的调用在MFC中相当频繁。这样,如果你调用该宏来验证其他线程对象的有效性,就会出现断言错误。就算不出现断言错误,也会导致程序异常退出,这是因为不允许直接操作其他线程中的CWnd对象。


The correct approach in such situations is to work with window handles, not MFC objects. It is safe to pass window handles across thread boundaries. If thread A passes a window handle to thread B, then thread B can use this window handle to send or post messages to the window. When these messages are processed, you are back in the context of thread A and calls to CWnd::AssertValid() to check thread A's window handle maps will succeed.
解决的方法是使用窗口句柄而不是MFC对象。在线程之间传递窗口句柄是安全的。如果线程A向线程B传递一个窗口句柄,那么,线程B可以通过发送消息给拥有该句柄的窗口对象。在处理窗口消息时,系统已经切换到线程A。这是验证窗口对象的有效性会成功。


In this scenario, thread B can also use the CWnd::FromHandle() function to get a temporary CWnd object which is placed in thread B's temporary handle map. However this object may be of only limited use, because in no way is it in synchronization with the MFC object existing in thread A's handle maps.
与此同时,线程B能够调用CWnd::FromHandle()函数来获得暂时的窗口对象,不过这一对象应该谨慎使用。因为它无法与存在于线程A窗口对象映射中的对象同步。

分享到:
评论

相关推荐

    深入浅出MFC【侯捷】

    正确态度 MFC多线程程序设计 探索CWinThread 产生一个Worker Thread 产生一个UI Thread 线程的结束 线程与同步控制 MFC多线程程序例程 第15章 定制一个AppWizard 到底Wizard是什么? Custom AppWizard的基本操作 ...

    深入浅出MFC 2e

    第14章 MFC多线程程序设计 从操作系统层面看线程 三个观念:模块、行程和线程 线程优先级(Priority) 线程调度(Scheduling) Thread Context 从程序设计层面看线程 Worker Threads和UI Threads 错误观念 正确态度 ...

    侯捷- -深入浅出MFC

    第14章 MFC多线程程序设计 从操作系统层面看线程 三个观念:模块、行程和线程 线程优先级(Priority) 线程调度(Scheduling) Thread Context 从程序设计层面看线程 Worker Threads和UI Threads 错误观念 正确态度 ...

    MFC教程入门知识全集.rar

    11.2.3 实例:通过创建多线程来编写网络聊天程序 11.3 MFC 的线程处理 11.3.1 创建工作者线程 11.3.2 创建用户界面线程 11.4 线程同步 11.4.1 为什么要同步 11.4.2 等待函数 11.4.3 同步对象 11.4.3.1 关键...

    VISUAL C++MFC扩展编程实例

    13.4 实例50:与其他应用程序共享数据 300 13.5 实例51:使用套接字与任意的应用 程序通信 301 13.6 实例52:使用串行或并行I/O 321 第14章 多任务 331 14.1 实例53:后台处理 331 14.2 实例54:运行其他应用程序 ...

    VISUAL C MFC扩展编程实例与源码

    13.4 实例50:与其他应用程序共享数据 300 13.5 实例51:使用套接字与任意的应用 程序通信 301 13.6 实例52:使用串行或并行I/O 321 第14章 多任务 331 14.1 实例53:后台处理 331 14.2 实例54:运行其他应用...

    一份很实用的MFC资料

    13.4 实例50:与其他应用程序共享数据 300 13.5 实例51:使用套接字与任意的应用 程序通信 301 13.6 实例52:使用串行或并行I/O 321 第14章 多任务 331 14.1 实例53:后台处理 331 14.2 实例54:运行其他应用程序 ...

    VISUAL C++MFC扩展编程实例(想学MFC的朋友一定不要错过)

    13.4 实例50:与其他应用程序共享数据 300 13.5 实例51:使用套接字与任意的应用 程序通信 301 13.6 实例52:使用串行或并行I/O 321 第14章 多任务 331 14.1 实例53:后台处理 331 14.2 实例54:运行其他应用程序 ...

    Visual C++ MFC扩展编程实例.PDF

    13.4 实例50:与其他应用程序共享数据 300 13.5 实例51:使用套接字与任意的应用 程序通信 301 13.6 实例52:使用串行或并行I/O 321 第14章 多任务 331 14.1 实例53:后台处理 331 14.2 实例54:运行其他应用程序 ...

    VC++ 编程指南_中文chm

    第一课 Windows编程和面向对象技术  1.1 Windows发展历史 ...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    vc++ 应用源码包_1

    在MFC应用程序中浏览PDF、Word文档文件 vcdialog 自绘对话框。 vc编程:自动停靠窗体,吸附窗体 OnMoving事件处理。 vc基于HTTP协议断点续传和多线程下载源代码 VC满屏开满玫瑰花 VC然输入框出现气球提示 调用...

    MFC程序员的WTL指南

    WTL有AppWizard,可以让您生成SDI, MDI 和多线程SDI程序多线程SDI跟IE或Windows Explorer很像.它看起来是打开了多个程序实例,实际上这些窗口都是属于一个进程的.另外,您的程序可以是基于对话框的,也可以是基于视的....

    VC++可视化编程指南

    第一课 Windows编程和面向对象技术  1.1 Windows发展历史 ...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    VC++编程指南(中文版).chm

    VC++编程指南(中文版),不错的一本电子书。啥也不说了,直接看内容预览...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    vc++ 应用源码包_2

    在MFC应用程序中浏览PDF、Word文档文件 vcdialog 自绘对话框。 vc编程:自动停靠窗体,吸附窗体 OnMoving事件处理。 vc基于HTTP协议断点续传和多线程下载源代码 VC满屏开满玫瑰花 VC然输入框出现气球提示 调用...

    vc++ 应用源码包_6

    独立打包,保证可解压,内含大量源码,网上搜集...在MFC应用程序中浏览PDF、Word文档文件 vcdialog 自绘对话框。 vc编程:自动停靠窗体,吸附窗体 OnMoving事件处理。 vc基于HTTP协议断点续传和多线程下载源代码 VC...

    vc++ 应用源码包_5

    独立打包,保证可解压,内含大量源码,网上搜集...在MFC应用程序中浏览PDF、Word文档文件 vcdialog 自绘对话框。 vc编程:自动停靠窗体,吸附窗体 OnMoving事件处理。 vc基于HTTP协议断点续传和多线程下载源代码 VC...

    vc++ 应用源码包_3

    在MFC应用程序中浏览PDF、Word文档文件 vcdialog 自绘对话框。 vc编程:自动停靠窗体,吸附窗体 OnMoving事件处理。 vc基于HTTP协议断点续传和多线程下载源代码 VC满屏开满玫瑰花 VC然输入框...

    vc++ 开发实例源码包

    精灵系统,一套MFC渲染引擎,含2D/3D等渲染,效果看源码,IFEngine是整个引擎接口,IFSystem是硬件查询系统,IFApplication是应用程序对象基类。 FlashPlayer播放器4.0的VC++源代码 如题。 FreeBird2011最初版(模仿...

    深入解析ATL(第2版).pdf

     ·使用ATL窗口类和控件建立独立的应用程序和UI组件。  ·使用ATL Servet开发可以运行在微软IIS上的Web应用程序。 虽然.NET系统从1998年才开始其发展进程。但它已经使很多开发人员的编程发生了革命性的变化,并在...

Global site tag (gtag.js) - Google Analytics