testDlg.cpp中:void CTestDlg::sign1(int number1) 
{
    char tmpstr[20];
sprintf(tmpstr, "%d", number1);
CWnd* pWnd = GetDlgItem(IDC_EDIT1);    
pWnd->SetWindowText(tmpstr);
}void CTestDlg::OnButton3() 
{
sign1(200) ;
}
sign1()调用正常;
在 MyThread.h  中:
#include "testDlg.h"
…………
// Attributes
public:
    
  CTestDlg *mainDlg;
…………但是在int MyThread::Run()中用
mainDlg->sign1(100) ;一运行到此,就会出错:
Unhandled exception in test.exe(MFC42D.DLL)0xC0000005 Access Violation.本人刚用VC++,请问何故,如何解决?

解决方案 »

  1.   

    in you thread function, you can not access  MFC Object
    you can sendmessage to the dialog!
      

  2.   

    问题是为何不能 access  MFC Object 呢?
    另外,比如我想在窗体的Edit控件中显示数据,
    又如何从线程发消息呀?
      

  3.   

    your can use your windows handle as parameter of your thread function.
    then in your thread function, you can use ::SendMessage send messge to your window.
    in your window,you handle the message, do somthing!
      

  4.   

    关键是:
    CTestDlg *mainDlg;
    mainDlg是NULL;如果你将CTestDlg *mainDlg;
    申请为全局变量,并且在CTestDlg的OnInitDialog()中mainDlg=this;
    并确保mythread在这以后才
    mainDlg->sign1(100) ;
      

  5.   

    内存非法访问,怀疑mainDlg为空指针或者错误指针,也有可能函数本身就有问题。另外,MFC对象不能在线程中传递,否则有可能会出错,具体原因参见《win32线程设计》:侯捷译