请教:多线程处理问题。

gerogeyzl 2003-10-20 06:12:59
线程函数如下:
UINT ThreadProc(LPVOID pParam)
{
((CMyClassDlg*)pParam)->Func();
return 1;
}

CMyClassDlg是基于对话框的类。
CMyClassDlg::Func()
{
//此函数被线程函数调用

 //出错的地方
UpdateData(); //出错(应用程序异常)
//同时调用AfxGetApp()得到主应用类的指针,如CMyClassApp* pApp = (CMyClassApp*)AfxGetApp();
//再引用pApp的指针成员时,出现内存错误

}

//按钮单击函数
CMyClassDlg::OnButton1()
{
AfxBeginThread(ThreadProc, this);
}

请问线程函数调用其它类的成员函数时,成员函数内的指针变量如何处理?UpdateData()好像也不能调用??
...全文
49 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lop5712 2003-10-24
  • 打赏
  • 举报
回复
楼主的问题我已经在下面这个帖子中详细作出过说明
http://expert.csdn.net/Expert/topic/2342/2342433.xml?temp=4.565066E-02

对于楼主现在的这种情况,正确的处理方法就如楼上所说,自定义一个消息,将原来的成员函数变成消息响应函数。不过并不是如楼上所说的不能调用其他类的成员函数,而是不能调用MFC包装类的派生类的成员函数。
lzzqqq 2003-10-23
  • 打赏
  • 举报
回复
你在启动线程时ThreadProc(LPVOID pParam)
pParam传入的参数是不是 (LPVOID)this ??
如果是的话应该不会出现你说的问题。
gambolgs 2003-10-23
  • 打赏
  • 举报
回复
不要直接用线程调用其他类的成员函数,而应该使用消息传递的方法。
bullfinch 2003-10-23
  • 打赏
  • 举报
回复
原理上不行
没看到mfc的注释吗?

// 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.
xhwu3721 2003-10-23
  • 打赏
  • 举报
回复
你要用線程消息,在線程中使用UpdateData()會出問題,我試過很多了,,
不知哪位大俠有更好的方法?
seacode 2003-10-23
  • 打赏
  • 举报
回复
try following code?
CMyClassDlg *pThis = (CMyClassDlg*)pParam;
pThis->YourFunc();
孤必有邻 2003-10-20
  • 打赏
  • 举报
回复
不存在你所说的指针变量处理问题,估计问题处在对话框指针上,你的指针可能为空,原因可能是调用时机不对。

15,471

社区成员

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

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