为什么在线程中调用主对话框函数时会出错?看一下我的代码
UINT MyRcvThread(LPVOID pParam)
{
CMyDlg *p=(CMyDlg *)pParam;
p->showtext("kkk");
//debug assertion failed. file:wincore.cpp line :884
return 1;
}
void CMyDlg::OnOK()
{
AfxBeginThread(MyRcvThread,this,THREAD_PRIORITY_NORMAL);
}
void CMyDlg::showtext(CString str)
{
m_sRcv=str;
UpdateData(FALSE);
}
问题点数:40、回复次数:7Top
1 楼netboys(笨小孩)回复于 2002-04-12 08:35:19 得分 5
试试下面的:
UINT MyRcvThread(LPVOID pParam)
{
this->showtext("kkk");
//debug assertion failed. file:wincore.cpp line :884
return 1;
}Top
2 楼thindz(半截)回复于 2002-04-12 08:40:31 得分 0
不行的,全局函数没有this指针Top
3 楼djhdu(小火花)回复于 2002-04-12 09:32:09 得分 5
你的代码只有这些吗?Top
4 楼qunta(J2Y2)回复于 2002-04-12 09:52:22 得分 30
问题出在 UpdateData上,试试这个
void CMyDlg::showtext(CString str)
{
m_sRcv=str;
SetDlgItemText(IDC_SRCV, m_sRcv);
}
wincore.cpp line 886有如下说明:
// Note: if either of the above asserts fire and you are
// writing a multithreaded application, it is likely that
// you have passed a C++ object from one thread to another
// and have used that object in a way that was not intended.
// (only simple inline wrapper functions should be used)
//
// In general, CWnd objects should be passed by HWND from
// one thread to another. The receiving thread can wrap
// the HWND with a CWnd object by using CWnd::FromHandle.
//
// It is dangerous to pass C++ objects from one thread to
// another, unless the objects are designed to be used in
// such a manner.
Top
5 楼djhdu(小火花)回复于 2002-04-12 10:14:44 得分 0
我试了试,为什么一直提示 'CRDlg' : undeclared identifier
'p' : undeclared identifierTop
6 楼djhdu(小火花)回复于 2002-04-12 10:16:31 得分 0
应该是'CMyDlg':undeclared identifierTop
7 楼thindz(半截)回复于 2002-04-12 10:34:29 得分 0
那些英文几乎都认识,但连起来却不知道说什么了。
问题确实是在UPDATEDATA上。我只好用POSTMESSAGE了。
qunta(旺财1860)的例子我没试过但先谢谢了。Top




