CoMarshalInterThreadInterfaceInStream使用
来源:互联网 发布:js正则判断数字和字母 编辑:程序博客网 时间:2024/06/10 00:01
You have to marshal the IServiceProvider interface from your main thread to your background thread.
In your SetSite call (which is guaranteed to be called on the main UI thread), marshal the IServiceProvider interface into a shared stream (IStream) object using CoMarshalInterThreadInterfaceInStream. You’ll need to make sure that this shared object can be accessed from both threads. In your background thread, when you need to get at the IServiceProvider, call CoGetInterfaceAndReleaseStream to get back a proxied IServiceProvider that works for the calling thread. Note that you can call this only once, so cache the interface that you get back. You can then call through this interface to QueryService. See the example code below:
class CMyPackage : public IVsPacakge
{
private:
CComPtr< IStream > m_pSPStream; // Used to marshal IServiceProvider between threads
CComPtr< IServiceProvider > m_pBackgroundSP; // IServiceProvider proxy for the background thread
public:
HRESULT SetSite( IServiceProvider* pSP )
{
// Marshal the service provider into a stream so that
// the background thread can retrieve it later
CoMarshalInterThreadInterfaceInStream(IID_IServiceProvider, pSP, &m_pSPStream);
//... do the rest of your initialization
}
// Call this when your background thread needs to call QueryService
// The first time through, it unmarshals the interface stored
HRESULT QueryServiceFromBackgroundThread(
REFGUID rsid, // [in] Service ID
REFIID riid, // [in] Interface ID
void **ppvObj // [out] Interface pointer of requested service (NULL on error)
{
if( !m_pBackgroundSP )
{
if( !m_pSPStream )
{
return E_UNEXPECTED;
}
HRESULT hr = CoGetInterfaceAndReleaseStream( m_pSPStream, IID_IServiceProvider, (void **)&m_pBackgroundSP );
if( FAILED(hr) )
{
return hr;
}
// The CoGetInterfaceAndReleaseStream has already destroyed the stream.
// To avoid double-freeing, the smart wrapper needs to be detached.
m_pSPStream.Detach();
}
return m_pBackgroundSP->QueryService( rsid, riid, ppvObj );
}
};
- CoMarshalInterThreadInterfaceInStream使用
- CoMarshalInterThreadInterfaceInStream 注意几点
- 使用
- 使用
- 使用
- 使用
- 使用
- 使用
- 使用++,--
- 使用$@ $!
- 使用
- SoftICE使用(指令使用)
- 使用GraphEdit使用
- 使用HtmlParser使用心得
- 时钟使用使用
- Ubuntu 使用Git 使用
- Ubuntu 使用Git 使用
- GUID使用的使用
- ExtJS2.0实用简明教程
- 关于Ext的Tree带CheckBox问题的修改
- Windows Sockets 规范及应用
- 编程的误区,思维啊思维
- 仿163邮箱上传多附件,有点酷
- CoMarshalInterThreadInterfaceInStream使用
- C#手机应用工作笔记(一)
- Cuyahoga内容管理精髓
- 疯狂的程序员63
- Pear 的Excel类写数据需要注意的地方
- ASP.NET中应用Excel:(6)在服务器端生成HTML表格
- 完美解决dataset导出excel问题
- wappush
- Linux几个简单使用的命令