我在对话框类的函数A中调用了CreateThread函数,其线程函数为B,并传参数this指针给B,在B中调用this->UpdateData(FALSE)就死掉,为什么呢?

解决方案 »

  1.   

    CreateThread传递给B?  this能传递给B么?
      

  2.   

    DWORD dwThreadId;::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)B, (LPVOID)this, CREATE_SUSPENDED, &dwThreadId);::ResumeThread(dwThreadId);
    ....void B(CMyDlg* myDlg)
    {
        ....
        myDlg.UpdateData(FALSE); // exception!!!
    }
      

  3.   

    你把UpdateData(FALSE); 
    放到对话框类的函数A中调用了CreateThread函数
    的后面不行吗??干吗要在线程中update呢??如果一定要可能是句柄没传过去
      

  4.   

    void COneDlg::OnOK() 
    { //传句柄
     h=::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)B, this->m_hWnd,0 ,&dwThreadId);}
    //extern COneDlg * mydlg2;
    void B(HWND myDlg)
    {
        COneDlg *dlg;
    // 把句柄转换为对象
    dlg=(COneDlg *)CWnd::FromHandle(myDlg);
    dlg->UpdateData(FALSE);
       

    }至于为什么,参看 如下说明:( wincore.cpp,page 887)// 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.
      

  5.   

    建议你传句柄过去,........this->hWnd........UINT youThread ( LPVOID pParam )
    {
       CWnd * pWnd = CWnd::FromHandle((HWND)pParam);
       CWnd * pParent = pWnd->GetTopLevelParent();
       ... ... 
    }传指针不好控制!
      

  6.   

    UpdateData问题是没了
    但是不能用pWnd访问其成员变量,例如:
    pWnd->m_strData = "xxxx"; // 马上当掉