📄 csdn_文档中心_com样例(三)——组件端代码.htm
字号:
CExample1Logic::Task1()<BR>{<BR><SPAN style="COLOR: green">//
Sleep一段时间以模拟长时间的调用</SPAN><BR> <SPAN
style="COLOR: blue">for</SPAN>( DWORD i = 0; i < 30; i++
)<BR>
{<BR> Sleep( 300
);<BR> <SPAN
style="COLOR: blue">if</SPAN>( ::CoTestCancel() ==
RPC_E_CALL_CANCELED
)<BR>
<SPAN style="COLOR: blue">return</SPAN>
E_ABORT;<BR> }<BR> <SPAN
style="COLOR: blue">return</SPAN> S_OK;<BR>}<BR><BR>STDMETHODIMP
CExample1Logic::Task2()<BR>{<BR><SPAN style="COLOR: green">//
通过GIT获得正确的ITaskManager的代理</SPAN><BR>
ITaskNotify *pNotify = NULL;<BR>
ITaskManager *pManager = NULL;<BR><BR>
::EnterCriticalSection( &m_CS );<BR> <SPAN
style="COLOR: blue">if</SPAN>( m_Cookie != <SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1 )
)<BR>
{<BR> ATLASSERT( g_pGIT
);<BR> <SPAN style="COLOR: green">//
不论GetInterfaceFromGlobal是否成功,都必须完成工作</SPAN><BR>
<SPAN style="COLOR: blue">if</SPAN>( SUCCEEDED(
g_pGIT->GetInterfaceFromGlobal(<BR>
m_Cookie,<BR>
IID_ITaskManager,<BR>
<SPAN style="COLOR: blue">reinterpret_cast</SPAN>< <SPAN
style="COLOR: blue">void</SPAN>** >( &pManager ) ) )
)<BR>
pManager->QueryInterface(
IID_ITaskNotify,<BR>
<SPAN
style="COLOR: blue">reinterpret_cast</SPAN>< <SPAN
style="COLOR: blue">void</SPAN>** >( &pNotify )
);<BR> }<BR>
::LeaveCriticalSection( &m_CS );<BR><BR><SPAN
style="COLOR: green">// 开始任务</SPAN><BR> <SPAN
style="COLOR: blue">static</SPAN> DWORD index =
1;<BR> ITask *pTask = NULL;<BR>
CComObject< CTask > *pTaskObject = NULL;<BR>
<SPAN style="COLOR: blue">if</SPAN>( FAILED( CComObject< CTask
>::CreateInstance( &pTaskObject ) )
)<BR> <SPAN
style="COLOR: blue">return</SPAN> E_FAIL;<BR>
ATLVERIFY( SUCCEEDED(
pTaskObject->QueryInterface(<BR>
IID_ITask,<BR>
<SPAN style="COLOR: blue">reinterpret_cast</SPAN>< <SPAN
style="COLOR: blue">void</SPAN>** >( &pTask ) ) )
);<BR> DWORD cookie = <SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1
);<BR> WCHAR temp[10];<BR>
wsprintf( temp, L"任务二%d", index++ );<BR><BR>
pTaskObject->m_hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL
);<BR> <SPAN style="COLOR: blue">if</SPAN>(
pManager && FAILED( pManager->AddTask( temp, pTask,
&cookie ) ) )<BR>
cookie = <SPAN style="COLOR: blue">static_cast</SPAN>< DWORD
>( -1 );<BR> SafeRelease( pManager
);<BR><BR><SPAN style="COLOR: green">//
Sleep各段时间以模拟各步工作的完成</SPAN><BR> <SPAN
style="COLOR: blue">if</SPAN>( ::WaitForSingleObject(
pTaskObject->m_hEvent, 4000 ) == WAIT_OBJECT_0
)<BR> <SPAN
style="COLOR: blue">goto</SPAN> TERMINATE;<BR>
pTaskObject->m_Rate = 0.3f;<BR> <SPAN
style="COLOR: blue">if</SPAN>( pNotify
)<BR>
pNotify->ProcessRateChange( cookie );<BR><BR>
<SPAN style="COLOR: blue">if</SPAN>( ::WaitForSingleObject(
pTaskObject->m_hEvent, 4000 ) == WAIT_OBJECT_0
)<BR> <SPAN
style="COLOR: blue">goto</SPAN> TERMINATE;<BR>
pTaskObject->m_Rate = 0.6f;<BR> <SPAN
style="COLOR: blue">if</SPAN>( pNotify
)<BR>
pNotify->ProcessRateChange( cookie );<BR><BR>
<SPAN style="COLOR: blue">if</SPAN>( ::WaitForSingleObject(
pTaskObject->m_hEvent, 4000 ) == WAIT_OBJECT_0
)<BR> <SPAN
style="COLOR: blue">goto</SPAN> TERMINATE;<BR>
pTaskObject->m_Rate = 0.9f;<BR> <SPAN
style="COLOR: blue">if</SPAN>( pNotify
)<BR>
pNotify->ProcessRateChange( cookie );<BR><BR>
<SPAN style="COLOR: blue">if</SPAN>( pNotify )<BR>
{<BR> <SPAN
style="COLOR: blue">if</SPAN>( cookie != <SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1 )
)<BR>
pNotify->TaskOver( cookie
);<BR>
pNotify->Release();<BR> }<BR>
pTask->Release();<BR><BR> <SPAN
style="COLOR: blue">return</SPAN>
S_OK;<BR><BR>TERMINATE:<BR> <SPAN
style="COLOR: blue">if</SPAN>( pNotify )<BR>
{<BR> <SPAN
style="COLOR: blue">if</SPAN>( cookie != <SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1 )
)<BR>
pNotify->TaskOver( cookie
);<BR>
pNotify->Release();<BR> }<BR>
pTask->Release();<BR><BR> <SPAN
style="COLOR: blue">return</SPAN>
RPC_E_CALL_CANCELED;<BR>}<BR><BR><SPAN
style="COLOR: green">/////////////////////////////ISetTaskManager////////////////////////</SPAN><BR>STDMETHODIMP
CExample1Logic::SetTaskManager( ITaskManager *pManager
)<BR>{<BR> <SPAN style="COLOR: blue">if</SPAN>(
!pManager )<BR> <SPAN
style="COLOR: blue">return</SPAN> E_POINTER;<BR>
pManager->AddRef();<BR><BR> ATLASSERT( g_pGIT
);<BR><SPAN style="COLOR: green">//
注销原来的m_Cookie</SPAN><BR> ::EnterCriticalSection(
&m_CS );<BR> <SPAN
style="COLOR: blue">if</SPAN>( m_Cookie != <SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1 )
)<BR> ATLVERIFY(
SUCCEEDED(<BR>
g_pGIT->RevokeInterfaceFromGlobal( m_Cookie ) ) );<BR><BR><SPAN
style="COLOR: green">// 注册新的m_Cookie</SPAN><BR>
<SPAN style="COLOR: blue">if</SPAN>( FAILED(
g_pGIT->RegisterInterfaceInGlobal(
pManager,<BR>
IID_ITaskManager,<BR>
&m_Cookie ) ) )<BR>
::InterlockedExchange( <SPAN
style="COLOR: blue">reinterpret_cast</SPAN>< LONG* >(
&m_Cookie
),<BR>
<SPAN
style="COLOR: blue">static_cast</SPAN>< DWORD >( -1 )
);<BR> ::LeaveCriticalSection( &m_CS
);<BR><BR>
pManager->Release();<BR> <SPAN
style="COLOR: blue">return</SPAN>
S_OK;<BR>}</P></FONT><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_COM样例(三)——组件端代码.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_COM样例(三)——组件端代码.files/ico_pencil.gif" width=16>
</SPAN> freeshoot <I>(2004-4-27 17:42:42)</I>
</TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>支持,并感谢。
<BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>你没有登陆,无法发表评论。 请先<A
href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=27177">登陆</A>
<A
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文档中心_COM样例(三)——组件端代码.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -