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

zteeb 2003-08-22 10:54:27
我的程序是一个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.
...全文
77 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
numen27 2003-08-22
  • 打赏
  • 举报
回复
在线程里好像好多事情都不做的,我就碰到过类似的问题,你在线程里发送某个自定义消息,然后主程序接受那个消息,然后打开对话框
zteeb 2003-08-22
  • 打赏
  • 举报
回复
多谢各位支招,我的线程就是用CreateThead()建立的,应该不会涉及到用户界面线程和工作线程的区别吧?
to : farfh(慕容长风) :那domal()在哪里完成呢,是在新线程里,还是线程外?
farfh 2003-08-22
  • 打赏
  • 举报
回复
如果你一定要那样做,建议你先在线成外把窗口创建起来,SHOWHIDE,然后将窗口句丙或者DLG指针传入线成,SHOWTRUE,这样就成了。
farfh 2003-08-22
  • 打赏
  • 举报
回复
总之不要在线程中使用MFC对象就没错,呵呵,MFC的东西讨厌得紧,自己写个对话框吧。
benz600 2003-08-22
  • 打赏
  • 举报
回复
用create()函数试试

15,471

社区成员

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

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