COM线程模型 - MTA接口 - Part III -(STA套间调用MTA对象)

From : http://blog.csdn.net/zj510/article/details/39008107

 

那么如果在STA套间里面创建MTA对象,又如何?
看这段代码:
  1. // TestCom.cpp : Defines the entry point for the console application. 
  2. // 
  3.  
  4. #include "stdafx.h" 
  5. #include <atlbase.h> 
  6. #include <atlcom.h> 
  7. #include "../MyCOM/MyCOM_i.h" 
  8. #include "../MyCOM/MyCOM_i.c" 
  9.  
  10. #include <iostream> 
  11. #include <thread> 
  12.  
  13.  
  14. using namespace std; 
  15.  
  16. void WorkThread() 
  17.     CoInitialize(0); 
  18.  
  19.     WCHAR temp[100] = { 0 }; 
  20.     swprintf_s(temp, L"thread tid: %d\n", ::GetCurrentThreadId()); 
  21.     ::OutputDebugStringW(temp); 
  22.  
  23.     { 
  24.         CComPtr<IMyRect> spRect; 
  25.         HRESULT hr = spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER); 
  26.  
  27.         hr = spRect->Draw(CComBSTR(L"red")); 
  28.     } 
  29.  
  30.     CoUninitialize(); 
  31.  
  32. int _tmain(int argc, _TCHAR* argv[]) 
  33.     CoInitialize(0); 
  34.  
  35.     WCHAR temp[100] = {0}; 
  36.     swprintf_s(temp, L"main tid: %d\n", ::GetCurrentThreadId()); 
  37.     ::OutputDebugStringW(temp); 
  38.  
  39.     { 
  40.         CComPtr<IMyRect> spRect; 
  41.         spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER); 
  42.  
  43.         spRect->Draw(CComBSTR(L"red")); 
  44.  
  45.         CComPtr<IMyRect> spRect2; 
  46. spRect2.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);
  47.  
  48.         spRect2->Draw(CComBSTR(L"blue")); 
  49.     } 
  50.      
  51.     std::thread t(WorkThread); 
  52.     std::thread t1(WorkThread); 
  53.     std::thread t2(WorkThread); 
  54.  
  55.     CoUninitialize(); 
  56.  
  57.     t.join(); 
  58.     t1.join(); 
  59.     t2.join(); 
  60.  
  61.     return 0; 
// TestCom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlbase.h>
#include <atlcom.h>
#include "../MyCOM/MyCOM_i.h"
#include "../MyCOM/MyCOM_i.c"

#include <iostream>
#include <thread>


using namespace std;

void WorkThread()
{
    CoInitialize(0);

    WCHAR temp[100] = { 0 };
    swprintf_s(temp, L"thread tid: %d\n", ::GetCurrentThreadId());
    ::OutputDebugStringW(temp);

    {
        CComPtr<IMyRect> spRect;
        HRESULT hr = spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        hr = spRect->Draw(CComBSTR(L"red"));
    }

    CoUninitialize();
}

int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(0);

    WCHAR temp[100] = {0};
    swprintf_s(temp, L"main tid: %d\n", ::GetCurrentThreadId());
    ::OutputDebugStringW(temp);

    {
        CComPtr<IMyRect> spRect;
        spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        spRect->Draw(CComBSTR(L"red"));

        CComPtr<IMyRect> spRect2;
        spRect2.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        spRect2->Draw(CComBSTR(L"blue"));
    }
	
    std::thread t(WorkThread);
    std::thread t1(WorkThread);
    std::thread t2(WorkThread);

	CoUninitialize();

    t.join();
    t1.join();
    t2.join();

	return 0;
}

IMyRect是一个MTA接口。上面的代码运行结果如下:


红色部分是主线程输出的,主线程初始化STA套间,主线程id是7812,然后COM函数里面输出的是4936.那么说明COM并不是在主线程里面运行的。
绿色部分是3个辅助线程输出的,可以看到COM运行线程中,其中2个是一样的,另外一个不一样。

这就说明:
1. 当在STA套间里面创建MTA对象时,系统会自动创建一个MTA套间。
2. MTA对象在MTA套间里面运行。
3. 运行线程由系统来创建,系统会创建几个运行线程,并且挑选某个线程为当前调用服务。


相关资料:

http://support.microsoft.com/kb/150777

http://forums.codeguru.com/showthread.php?449647-STA-component-call-MTA-component

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值