0
votes

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.

2
UpdateData is designed to validate and exchange data values with the controls of a dialog. Your dialog doesn't even exist yet (as a window), much less any of its controls. And the stopping point of your assertion failure probably shows you this (and us if you posted it, which you didn't). I don't think you even need the local dlgObject. It appears you want to update the dialog that started this thread in the first place.WhozCraig

2 Answers

5
votes

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

2
votes

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();
}