::WaitForSingleobject(hThreadSTA,INFINITE); ::WaitForSingleobject (hThreadMTA,INFINITE); return 0; } long WINAPI MySTAThreadFunction(long lParam) { ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED); cout << "From .NET when called from the STA Worker Thread (Direct Access) : " << spHelloNET->GetThreadID() << endl; IHelloDotNetPtr spHello = NULL; HRESULT hr = CoGetInterfaceAndReleaseStream(g_pStream1, __uuidof(IHelloDotNet), (void **)&spHello); if(S_OK == hr) { cout << "From .NET when called from the STA Worker Thread (Marshaled) : " << spHello->GetThreadID() << endl; } return 0; } long WINAPI MyMTAThreadFunction(long lParam) { // 让线程进入MTA ::CoInitializeEx(NULL,COINIT_MULTITHREADED); cout << "From .NET when called from the MTA Worker Thread (Direct Access) : " << spHelloNET->GetThreadID() << endl; IHelloDotNetPtr spHello = NULL; HRESULT hr = CoGetInterfaceAndReleaseStream(g_pStream2, __uuidof(IHelloDotNet), (void **)&spHello); if(S_OK == hr) { cout << "From .NET when called from the MTA Worker Thread (Marshaled) : " << spHello->GetThreadID() << endl; } // 从线程退出 return 0; }/* 结束MyMTAThreadFunction */ 当运行控制台应用程序时,这是得到的输出。
The Thread ID of the primary STA thread is : 2220 From .NET when called from the primary STA Thread : 2220 The Thread ID of the STA based Worker thread is : 2292 The Thread ID of the MTA based Worker thread is : 2296 From .NET when called from the STA Worker Thread (Direct Access) : 2292 From .NET when called from the STA Worker Thread (Marshalled) : 2292 From .NET when called from the MTA Worker Thread (Direct Access) : 2296 From .NET when called from the MTA Worker Thr