📄 subject_62821.htm
字号:
<p>
序号:62821 发表者:mwh 发表日期:2003-11-28 21:07:34
<br>主题:线程关闭问题
<br>内容:我在程序中开启了两个用户线程,在每次断开套接字时关闭线程,为做测试,我循环打开连接,从windows资源管理器中观察,发现随着次数增加,运行的线程数也在增加,说明线程没有关闭。但我每次关闭连接时(中断线程),在线程的析构函数中的消息框均会弹出,为什么?程序如下:<BR>...........................<BR>CWinThread * m_threadPtr; //监听线程指针<BR>CWinThread * m_messthreadptr; //消息处理线程指<BR><BR>....................<BR>void CGzxfDlg::OpenThread()<BR>{<BR> m_threadPtr = AfxBeginThread(RUNTIME_CLASS(CThread));<BR> m_messthreadptr = AfxBeginThread(RUNTIME_CLASS(CMessThread)); <BR> }<BR><BR><BR>void CGzxfDlg::CloseThread()<BR>{<BR> m_threadPtr->PostThreadMessage(WM_QUIT,0,0); //关闭线程<BR> delete m_threadPtr;<BR> m_threadPtr=NULL;<BR> m_messthreadptr->PostThreadMessage(WM_QUIT,0,0);<BR> delete m_messthreadptr;<BR> m_messthreadptr=NULL;<BR>}<BR><BR>void CGzxfDlg::OnCon() <BR>{<BR> ................<BR> OpenThread();<BR> .................<BR>}<BR><BR>afx_msg void CGzxfDlg::disconnection(WPARAM wParam, LPARAM lParam)<BR>{<BR> ...................................<BR><BR> Releasesocket();<BR> ..........................<BR>}<BR><BR><BR>//测试用,<BR>void CGzxfDlg::OnButton1() <BR>{<BR> int o=0;<BR> while(o<10000)<BR> {<BR> OnCon();<BR> Sleep(3000);<BR> PostMessage(DISCONNECT,0,0);<BR> Sleep(3000);<BR> o++;<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>
回复者:120 回复日期:2003-11-28 22:38:02
<br>内容:楼上的,你的问题正是我现在刚想提的<BR><BR>可以用<BR>AfxEndThread( UINT ExitCd )强制关闭每个线程<BR>ExitCd就是要关闭线程的ID<BR><BR>我没有msdn不知道这个id的值要在哪里取得?<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>
回复者:mwh 回复日期:2003-11-28 23:04:34
<br>内容:线程参数:m_nThreadID<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>回复者:太平冰水 回复日期:2003-11-29 09:52:12
<br>内容: m_threadPtr->PostThreadMessage(WM_QUIT,0,0); //关闭线程<BR>//要给一定的时间等待线程结束,最好使用信号灯控制<BR> delete m_threadPtr;<BR> m_threadPtr=NULL;<BR><BR>但是你所说的文的与这无关,windows在线程启动时修改进程控制块中的线程数,线程结束时他就不知道了,所以没有相应的减少,你应该在线程结束时调用一下ExitThread (在线程内部,通知windows有线程结束了
<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 + -