📄 subject_33989.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 WM_USER+1<BR>#define WM_PLAYCLIP WM_USER+2<BR>#define WM_SHOWFIRSTFRAME WM_USER+3<BR>#define WM_SHOWLASTFRAME WM_USER+4<BR><BR>UINT MyClipThreadProc( LPVOID pParam )<BR>{<BR> // NOTE: pParentWnd is the parent window of the animation control.<BR> CWnd* pParentWnd = (CWnd*) pParam;<BR> CAnimateCtrl cAnimCtrl;<BR><BR> // Create the animation control.<BR> if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER, <BR> CRect(10,10,100,100), pParentWnd, 1))<BR> return false;<BR><BR> // Open the AVI file.<BR> if (!cAnimCtrl.Open(_T("myavi.avi")))<BR> return false;<BR><BR> // Pump message from the queue until the stop play message is received.<BR> MSG msg;<BR> while (GetMessage(&msg, NULL, 0, 0) && (msg.message != WM_STOPCLIP))<BR> {<BR> switch (msg.message)<BR> {<BR> // Start playing from the first frame to the last, <BR> // continuously repeating.<BR> case WM_PLAYCLIP:<BR> if (!cAnimCtrl.Play(0, -1, -1))<BR> return false;<BR> break;<BR> <BR> // Show the first frame.<BR> case WM_SHOWFIRSTFRAME:<BR> if (!cAnimCtrl.Seek(0))<BR> return false;<BR> cAnimCtrl.RedrawWindow();<BR> break;<BR><BR> // Show the last frame.<BR> case WM_SHOWLASTFRAME:<BR> if (!cAnimCtrl.Seek(-1))<BR> return false;<BR> cAnimCtrl.RedrawWindow();<BR> break;<BR> }<BR><BR> TranslateMessage(&msg);<BR> DispatchMessage(&msg);<BR> }<BR><BR> cAnimCtrl.Stop();<BR> cAnimCtrl.Close();<BR><BR> 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> CWnd* pParentWnd = (CWnd*) pParam;<BR> CAnimateCtrl cAnimCtrl;<BR><BR> // Create the animation control.<BR> if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER, <BR> CRect(10,10,100,100), pParentWnd, 1))<BR> return false;<BR><BR> // Open the AVI file.<BR> if (!cAnimCtrl.Open(_T("myavi.avi")))<BR> 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 + -