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

📄 subject_15016.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:15016 发表者:lyw 发表日期:2002-09-13 15:34:18
<br>主题:一个多线程的问题?
<br>内容:我的程序基于多文档的MFC程序,采用VC6.0<BR>我在程序中创建了一个工作者线程,代码如下:<BR>MainFrm.h:<BR>#define WM_SENDCLIENTMSG&nbsp;&nbsp;WM_USER+300<BR>class CMainFrame : public CMDIFrameWnd<BR>{<BR>&nbsp;&nbsp;......<BR>public:<BR>&nbsp;&nbsp;CWinThread&nbsp;&nbsp;&nbsp;&nbsp;*SendMsgThread;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;void&nbsp;&nbsp;&nbsp;&nbsp;SendClientMsg();<BR>&nbsp;&nbsp;static&nbsp;&nbsp;UINT SendMsg(LPVOID lpparameter);<BR>private:<BR>&nbsp;&nbsp;UINT m_nTimer;<BR>&nbsp;&nbsp;.....<BR>}<BR><BR>MainFrm.cpp:<BR>....<BR>IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)<BR><BR>BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)<BR>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_MSG_MAP(CMainFrame)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ON_WM_SYSCOMMAND()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ........<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //}}AFX_MSG_MAP<BR>&nbsp;&nbsp;&nbsp;&nbsp;ON_MESSAGE(WM_SENDCLIENTMSG,SendClientMsg)<BR>END_MESSAGE_MAP()<BR><BR>UINT CMainFrame::SendMsg(LPVOID lpparameter)<BR>{<BR>&nbsp;&nbsp; ::SendMessage(((CMainFrame *)AfxGetMainWnd())-&gt;m_hWnd, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_SENDCLIENTMSG,0,0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; return 0;<BR>}<BR><BR>void CMainFrame::SendClientMsg()<BR>{<BR>&nbsp;&nbsp; CString strbuf;<BR>&nbsp;&nbsp; ........&nbsp;&nbsp;&nbsp;&nbsp;<BR>}<BR><BR>void CMainFrame::OnTimer(UINT nIDEvent) <BR>{&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; DWORD code;<BR><BR>&nbsp;&nbsp; GetExitCodeThread(SendMsgThread-&gt;m_hThread,&code);<BR>&nbsp;&nbsp; if(code==STILL_ACTIVE)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp; CMDIFrameWnd::OnTimer(nIDEvent);<BR>&nbsp;&nbsp;&nbsp;&nbsp; return;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; KillTimer(m_nTimer);<BR>&nbsp;&nbsp; m_nTimer=0;<BR>&nbsp;&nbsp; SendMsgThread=AfxBeginThread(CMainFrame::SendMsg,NULL,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;THREAD_PRIORITY_HIGHEST,0,0,NULL);<BR>&nbsp;&nbsp; if(SendMsgThread==NULL)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp; AfxMessageBox("创建发送信息线程失败!");<BR>&nbsp;&nbsp;&nbsp;&nbsp; delete SendMsgThread;<BR>&nbsp;&nbsp;&nbsp;&nbsp; SendMsgThread=NULL;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; CMDIFrameWnd::OnTimer(nIDEvent);<BR>&nbsp;&nbsp; m_nTimer=SetTimer(1,5000,0);<BR>}<BR><BR>void CMainFrame::OnLoginServer()<BR>{<BR>&nbsp;&nbsp;SendMsgThread=AfxBeginThread(CMainFrame::SendMsg,this,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;THREAD_PRIORITY_HIGHEST,0,0,NULL);<BR>&nbsp;&nbsp;if(SendMsgThread==NULL)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp; AfxMessageBox("创建发送信息线程失败!\n请重新登录.");<BR>&nbsp;&nbsp;&nbsp;&nbsp; delete SendMsgThread;<BR>&nbsp;&nbsp;&nbsp;&nbsp; SendMsgThread=NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp; m_bConnectServer=FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp; LoginSocket.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp; return ;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;m_nTimer=SetTimer(1,5000,0);<BR>}<BR><BR>问题是这样的:<BR>我用OnLoginServer()第一次创建线程没问题,<BR>但在运行到OnTime中创建线程后执行线程函数SendMsg时(即第二次执行线程函数SendMsg时)情况如下:<BR>如果在DEBUG方式下,那一点问题也没有,<BR>但在Release方式下就出现问题:<BR>"0x004273a2"指令引用的"0x9510028e"内存,该内存不能为“read",要终止程序,请单击"确定".<BR>我知道问题出在<BR>::SendMessage(((CMainFrame *)AfxGetMainWnd())-&gt;m_hWnd, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WM_SENDCLIENTMSG,0,0);<BR>语句上(在Release方式下多次调用该语句就不行),但不知怎么解决?<BR><BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:小戟 回复日期:2002-09-13 15:38:53
<br>内容:class CMainFrame : public CMDIFrameWnd<BR>{<BR>&nbsp;&nbsp;......<BR>public:<BR>&nbsp;&nbsp;CWinThread&nbsp;&nbsp;&nbsp;&nbsp;*SendMsgThread;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;void&nbsp;&nbsp;&nbsp;&nbsp;SendClientMsg();<BR>&nbsp;&nbsp;<BR>private:<BR>&nbsp;&nbsp;UINT m_nTimer;<BR>&nbsp;&nbsp;.....<BR>}<BR><BR>UINT SendMsg(LPVOID lpparameter);
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:lyw 回复日期:2002-09-16 09:51:51
<br>内容:不行,还是出现同样的问题!!小戟,难道在你的机器上能成功吗?注意要在Release方式下才会出现问题,在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>
回复者:hh 回复日期:2002-09-16 10:53:21
<br>内容:可能问题出在SendClientMsg消息处理函数上,你按标准的消息处理函数定义试试
<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>回复者:Aikoc 回复日期:2002-09-16 15:45:44
<br>内容:afx_msg HRESULT SendClientMsg(WPARAM wParam,LPARAM lParam);<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>
回复者:lyw 回复日期:2002-09-16 16:29:24
<br>内容:终于搞定,Aikoc,太谢谢你了,不过我还是搞不懂原因:<BR>为什么一定要加上WPARAM wParam,LPARAM lParam这两个参数?<BR>我用<BR>afx_msg void SendClientMsg();<BR>就不行,而用<BR>afx_msg void SendClientMsg(WPARAM wParam,LPARAM lParam);<BR>就行,<BR>而且为什么一定要加上afx_msg呢?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Aikoc 回复日期:2002-09-16 17:20:01
<br>内容:在Debug版本中,VC会处理wParam,及lParam的参数调用.<BR>但在Release,不会自动帮你处理.<BR>当system收到WM_SENDCLIENTMSG消息,并将wParam,lParam<BR>压栈,再掉用你的SendClientMsg时,因你声明的SendClientMsg没有<BR>参数,所以Ret时堆栈会发生错误.<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>
回复者:lyw 回复日期:2002-09-17 08:46:47
<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>
回复者:lyw 回复日期:2002-09-17 09:02:30
<br>内容:对了,afx_msg不加也可以
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Aikoc 回复日期:2002-09-17 09:36:00
<br>内容:// Type modifier for message handlers<BR>#ifndef afx_msg<BR>#define afx_msg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // intentional placeholder<BR>#endif<BR>afx_msg只是用来标明是消息处理函数
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

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