Microsoft KB Archive/828643

= MFC application stops responding when you initialize the application as a multithreaded apartment in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 828643

Article Last Modified on 1/17/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





SYMPTOMS
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
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
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.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create, and then build an MFC Single-Document Interface (SDI) application or an MFC Multiple-Document Interface (MDI) application by using the MFC AppWizard. To do this, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click MFC Application under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click MFC Application under Templates. In the Name text box, type Test, and then click OK.</li> In the MFC Application Wizard dialog box, click Application Type in the left pane.</li> Under Application Type, click Single document or Multiple documents, and then click Finish.</li></ol> </li>  In the InitInstance method of the Test.cpp file, locate the following code: return TRUE; </li>  Add the following code before the code that you located in the previous step: HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED ); if ( FAILED( hr ) ) {   AfxMessageBox(&quot;CoInitializeEx initialization failed&quot;); return FALSE; } Note In Visual Studio 2005, use the following code. HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED ); if ( FAILED( hr ) ) {   AfxMessageBox(L&quot;CoInitializeEx initialization failed&quot;); return FALSE; } </li> Build the application.</li> Press Ctrl+F5 to run the application. You notice the behavior that is mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

Keywords: kbbug kbthread KB828643

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.