MFC 与 COINIT_MULTITHREADED 冲突

330 篇文章 4 订阅 ¥19.90 ¥99.00
本文探讨了MFC应用程序在使用COINIT_MULTITHREADED时遇到的问题及解决方案,涉及线程行为和Microsoft UI交互。
摘要由CSDN通过智能技术生成

SYMPTOMS

<script type="text/javascript">loadTOCNode(1, 'symptoms');</script>
When you use the following CoInitializeEx method call to initialize a Microsoft Foundation Class Library (MFC) application as a multithreaded apartment (MTA), the application may stop responding:
CoInitializeEx(NULL, COINIT_MULTITHREADED)

CAUSE

<script type="text/javascript">loadTOCNode(1, 'cause');</script>
An MFC application uses a lot of thread local storage to store thread state information and to process state information. Therefore, an MFC application is not compatible with a free threaded programming model. For example, if you write an application that displays a user interface (UI) and that also uses some remote Component Object Model (COM) objects, during any method calls to the remote COM objects, the UI may stop responding. This behavior occurs because the UI thread that you initialize as an MTA thread by using the CoInitializeEx(..., COINIT_MULTITHREADED) method makes blocking Remote Procedure Calls (RPCs) to another COM apartment. While the UI thread waits for the RPC response message, any window messages that are posted to the queue of the UI thread are accumulated. Therefore, windows that were created by the UI thread may stop responding. This behavior may cause end users to prematurely quit the application.

RESOLUTION

<script type="text/javascript">loadTOCNode(1, 'resolution');</script>
To work around this problem, run the MFC application (UI thread) in a Single Threaded Apartment (STA). To do this, initialize the MFC application by using the CoIntializeEx(..., COINIT_APARTMENTTHREADED ) method. When an STA thread makes an RPC call, the thread drops into a COM-managed message loop (to dispatch window messages), and COM creates another thread to receive the RPC response message. Because the COM-managed message loop supports the dispatch of window messages, the UI can keep running. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值