简体   繁体   中英

MFC - UpdateData(False) + Thread + Debug assertion failed

Im using Visual Studio 2010, work with MFC 2008/2010. I have a problem with THREAD and UPDATEDATA(FALSE) This is init function

BOOL CBkav_btap2_appDlg::OnInitDialog(){
     ....
     AfxBeginThread (MyThreadProc,(LPVOID)GetSafeHwnd());
     return TRUE;  // return TRUE  unless you set the focus to a control

}

This is my THREAD

UINT __cdecl MyThreadProc( LPVOID pParam )
{
    DWORD totalphys;
    DWORD availablephys;
    DWORD memoload;
    CBT2Class* pObject = (CBT2Class*)pParam;
    pObject->GetRAMandCPUInfo(totalphys,availablephys,memoload  );
    CBkav_btap2_appDlg dlgObject;
    dlgObject.ec_totalphys = totalphys;

    dlgObject.UpdateData(FALSE);<--- Can not update data
    return 0;   
}

CBT2Class is the class in dll file i created before. ec_totalphys is just an edit_control. When i run, it return "Debud Assertion failed". I dont know why. Please help me. Thankss. p/s: I think i need use SendMessage to update data for Dialog but i search every where but still can't work.

You are passing an HWND as the thread parameter. It is not a pointer and you should not cast it to anything. You can use the HWND to post a custom message to the dialog. This custom message can include data in wParam and lParam. The message handler in the dialog runs in the main thread and can do the UpdateData call. See the example here for posting a custom message to the dialog: http://vcfaq.mvps.org/mfc/12.htm

OK. Thanks all for suggestions :D. So my particular solution is :

B1. Defined a MESSAGE

#ifdef _DEBUG
#define new DEBUG_NEW
#define WM_MY_MESSAGE (WM_USER+1000) // Cho chay o 2 thoi diem khac nhau 
#endif

B2. Signed in a MESSAGE MAP

BEGIN_MESSAGE_MAP(CBkav_btap2_appDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_MESSAGE(WM_MY_MESSAGE, TestMessageCall)
    ON_WM_TIMER()
END_MESSAGE_MAP()

B33. Create Thread

BOOL CBkav_btap2_appDlg::OnInitDialog()
{
    ..

    // TODO: Add extra initialization here

    AfxBeginThread(TestMethodThreadCall, (LPVOID)GetSafeHwnd());
    return TRUE;  // return TRUE  unless you set the focus to a control
}

B4. Create Method

UINT __cdecl TestMethodThreadCall( LPVOID pParam )
{
    while(1){
         //Ten Chuong trinh dc su dung nhieu nhat 
        HWND hDlg = (HWND )pParam;
        CString nameTestMessage = __T("Call From Message");
        ::SendMessage(hDlg, WM_MY_MESSAGE, (WPARAM)(&nameTestMessage), 0);          
        Sleep(5000);
    }
    return 0;
}

B5. Create Method Message call

LRESULT CBkav_btap2_appDlg::TestMessageCall(WPARAM wpD, LPARAM lpD)
{
    CString *pwpD = (CString *)wpD;
    ec_nameTestmessage  = *pwpD;
    UpdateData(FALSE);
    return LRESULT();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM