难题,新线程中定义一个对话框,在doMal()时失败

zteeb 2003-08-22 11:29:51


我的程序是一个MFC对话框程序,需要在按下某个Button后,启动另一个线程,并且在这个新线程中在定义一个对话框someDlg供用户填写一些东西,但运行someDlg.DoModal()时,总会在wincore.cpp的CWnd::AssertValid() 函数中的下列地方出现错误,即p!=this,我把someDlg定义为全局变量也同样通不过。后来发现在整个Assert下面有如下注释,好像是说这个错误是由于我在两个线程间传递Cwnd对象造成的,但我的someDlg并没有在两个线程间传递啊?有哪个高手可以解释下面的意思吗?

ASSERT((CWnd*)p == this); // must be us

// 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.

...全文
69 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cline 2003-08-22
  • 打赏
  • 举报
回复
use "AfxBeginThread()"
wangweintk 2003-08-22
  • 打赏
  • 举报
回复
从新线程发送一个消息给主线程,让它定义和打开这个新对话框,这才是常规的做法。
zteeb 2003-08-22
  • 打赏
  • 举报
回复
多谢各位,我用AfxBeginThread建立一个界面线程,解决了问题

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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