afxbeginthread 为什么在线程中访问界面元素时就停止响应了?

nanfeng231 2007-07-16 11:47:56
我的定义,MyThreadProc 是线程函数
class CMyDlg : public CDialog
{
.........
public:
static UINT MyThreadProc( LPVOID pParam );
.........
}
这里是调用:
CWinThread* pThread = AfxBeginThread(MyThreadProc, LPVOID(this));

//等待线程结束
WaitForSingleObject(pThread->m_hThread,INFINITE);


这是实现线程函数
UINT CMyDlg ::MyThreadProc( LPVOID pParam )
{
CMyDlg * pObject = (CMyDlg *)pParam;

pObject->m_combo_saveformat.GetWindowText(szExt);
//当我调用上面这句时,停止响应了,不知道什么原因,

。。。。。。。。。。。。。。。

}

真是郁闷,不知道是什么原因,就是不响应,访问别的界面元素也是啊。
那位大哥能帮忙指教一下,不胜感激阿!
...全文
282 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanfeng231 2007-07-16
  • 打赏
  • 举报
回复
WaitForSingleObject(pThread->m_hThread,INFINITE);
替换成
MSG message;
while(::PeekMessage(&message,NULL,0,0,PM_REMOVE)){
::TranslateMessage(&message);
::DispatchMessage(&message);
}
可以了
nanfeng231 2007-07-16
  • 打赏
  • 举报
回复
pObject->m_combo_saveformat.GetWindowText(szExt);在这里停止了
Big_Stone 2007-07-16
  • 打赏
  • 举报
回复
应该是你的线程崩溃了吧。怎么会停止响应呢。设置断点
pObject->m_combo_saveformat.GetWindowText(szExt);
你这样做其实也不合理的,你的是工作线程,不是UI线程。这样的话无疑就和UI产生了强烈的偶合。你可以使用线程拥有者的一个方法来取得你想要的数据。这些操作使用一个公共接口来实现。
knightshenbin 2007-07-16
  • 打赏
  • 举报
回复
停止响应?不跑了?停在那里?还是出错退出?
nanfeng231 2007-07-16
  • 打赏
  • 举报
回复
感谢各位
nanfeng231 2007-07-16
  • 打赏
  • 举报
回复
lyg_zy(学无止境,宁静致远)说的很对
nanfeng231 2007-07-16
  • 打赏
  • 举报
回复
这个低级的错误就出现在WaitForSingleObject(pThread->m_hThread,INFINITE);
因为程序在这里等待了
那末pObject->m_combo_saveformat.GetWindowText(szExt);就不起作用了
lyg_zy 2007-07-16
  • 打赏
  • 举报
回复
1:原因分析
nanfeng231(木木) 说得对,因为GetWindowText是通过给UI线程发WM_GETTEXT 去得到文本的,msdn解释如下:

If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without hanging if the process that owns the target window is hung. However, if the target window is hung and it belongs to the calling application, GetWindowText will hang the calling application.

而你的UI线程用WaitForSingleObject()阻塞了线程,所以当然死掉了。

2:解决问题的最好办法,楼上的同志们都说了:
这样做其实也不合理的,你的是工作线程,不是UI线程。这样的话无疑就和UI产生了强烈的偶合。你可以使用线程拥有者的一个方法来取得你想要的数据。这些操作使用一个公共接口来实现


16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧