⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_33989.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:33989 发表者:TheCold 发表日期:2003-03-25 18:30:30
<br>主题:请教:与work线程的通信问题?
<br>内容:请问,我在MSDN里面看到一个例子。<BR>// This example creates a secondary thread that implements<BR>// the methods of CAnimateCtrl. The procedure of the thread<BR>// is MyClipThreadProc and the thread was created with the<BR>// code AfxBeginThread( MyClipThreadProc, (LPVOID) pParentWnd).<BR>// The example code creates and initializes an animation control,<BR>// then proceeds to pump messages from the queue until one the <BR>// private messages WM_STOPCLIP, WM_PLAYCLIP, WM_SHOWFIRSTFRAME or <BR>// WM_SHOWLASTFRAME is received. The appropriate action is done for<BR>// these messages. The thread ends when the WM_STOPCLIP is received.<BR>// NOTE: the thread parameter, pParam, is a pointer to a CWnd object<BR>// that will be the parent of the animation control. <BR><BR>#define WM_STOPCLIP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_USER+1<BR>#define WM_PLAYCLIP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_USER+2<BR>#define WM_SHOWFIRSTFRAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_USER+3<BR>#define WM_SHOWLASTFRAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_USER+4<BR><BR>UINT MyClipThreadProc( LPVOID pParam )<BR>{<BR>&nbsp;&nbsp; // NOTE: pParentWnd is the parent window of the animation control.<BR>&nbsp;&nbsp; CWnd* pParentWnd = (CWnd*) pParam;<BR>&nbsp;&nbsp; CAnimateCtrl cAnimCtrl;<BR><BR>&nbsp;&nbsp; // Create the animation control.<BR>&nbsp;&nbsp; if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CRect(10,10,100,100), pParentWnd, 1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR><BR>&nbsp;&nbsp; // Open the AVI file.<BR>&nbsp;&nbsp; if (!cAnimCtrl.Open(_T("myavi.avi")))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<BR><BR>&nbsp;&nbsp; // Pump message from the queue until the stop play message is received.<BR>&nbsp;&nbsp; MSG msg;<BR>&nbsp;&nbsp; while (GetMessage(&amp;msg, NULL, 0, 0) &amp;&amp; (msg.message != WM_STOPCLIP))<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (msg.message)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Start playing from the first frame to the last, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // continuously repeating.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case WM_PLAYCLIP:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!cAnimCtrl.Play(0, -1, -1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Show the first frame.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case WM_SHOWFIRSTFRAME:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!cAnimCtrl.Seek(0))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cAnimCtrl.RedrawWindow();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Show the last frame.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case WM_SHOWLASTFRAME:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!cAnimCtrl.Seek(-1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cAnimCtrl.RedrawWindow();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TranslateMessage(&amp;msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DispatchMessage(&amp;msg);<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; cAnimCtrl.Stop();<BR>&nbsp;&nbsp; cAnimCtrl.Close();<BR><BR>&nbsp;&nbsp; return true;<BR>}<BR><BR>然后我在OnIniDialog()里启动线程。<BR><BR>用SendMessage() or PostThreadMessage() 来给线程发送那些自定义消息就是不行。<BR>线程函数里 消自LOOP根本就得不到消息。请问该怎么办呢?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:dr0 回复日期:2003-03-25 22:18:26
<br>内容:SendMessage() 肯定不行,你自己想想就明白了.<BR>PostThreadMessage() 不能够post和Window关联的message,也就是说DispathMessage<BR>无用,消息会丢失。<BR><BR>.<BR>&nbsp;&nbsp; CWnd* pParentWnd = (CWnd*) pParam;<BR>&nbsp;&nbsp; CAnimateCtrl cAnimCtrl;<BR><BR>&nbsp;&nbsp; // Create the animation control.<BR>&nbsp;&nbsp; if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CRect(10,10,100,100), pParentWnd, 1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR><BR>&nbsp;&nbsp; // Open the AVI file.<BR>&nbsp;&nbsp; if (!cAnimCtrl.Open(_T("myavi.avi")))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<BR><BR>跨越线程设置parent Window本身就是个错误,因为MESSAGE QUEUE是线程相关的.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:dr0 回复日期:2003-03-25 22:21:17
<br>内容:看看PostThreadMessage()的MSDN的说明,还有,注意Create, Open等等<BR>MFC类的函数调用可能会蕴含SendMessage, PostMessage, 等等调用,<BR>MessageBox,等等含有自己的message-pump, 这些都要考虑的.<BR>都会对你的结果产生影响。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:TheCold 回复日期:2003-03-26 13:30:08
<br>内容:那该怎么办?<BR>//跨越线程设置parent Window本身就是个错误<BR>不这样做怎么传递一个Dialog 句柄给它呢?那个例子要求这样做。<BR><BR>要用到thread-specific hook吗?截取消息我不会。<BR><BR>刚开始研究多线程,请多多指点?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:dr0 回复日期:2003-03-26 18:36:13
<br>内容:方便的话,你把代码zip上来,最近刚好有空,如果你愿意,我可以帮你debug一下.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:TheCold 回复日期:2003-03-27 14:19:49
<br>内容:感谢!已传上。。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:dr0 回复日期:2003-03-27 23:20:35
<br>内容:PostThreadMessage() 把消息post给自己了....<BR>修改的文件:
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:dr0 回复日期:2003-03-27 23:40:49
<br>内容:还有纠正你的一个措辞,"work线程"<BR>这个线程有MESSAGE QUEUE, MESSAGE PUMP,Window不能称之为worker thread了。<BR>2003-3-27 23:58:41

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -