'How can I use UpdateData func in controlling function (UINT) in MFC App?
I'm creating a multi-threading program with MFC using controlling function is UINT.
Now I want to update GUI using UpdateData(FALSE), but visual studio 2022 say that
- a nonstatic member reference must be relative to a specific object
- CWnd::UpdateData': illegal call of non-static member function
Here is my UINT code:
UINT MyThreadProc(LPVOID Param) {
CFolderPickerDialog m_dlg;
m_dlg.m_ofn.lpstrTitle = _T("Select Folder To Scan");
m_dlg.m_ofn.lpstrInitialDir = _T("C:\\");
if (m_dlg.DoModal() == IDOK) {
m_Path = m_dlg.GetPathName();
m_Path += _T("\\");
CWnd::UpdateData(FALSE);
}
}
void CMultithreadDlg::OnBnClickedButtonBrowse(){
AfxBeginThread(MyThreadProc,this);
UpdateData(FALSE);
GetDlgItem(IDC_BUTTON_Browse)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_PATH)->EnableWindow(FALSE);
}
How can I fix this, thanks
Solution 1:[1]
Let's consider that CFolderPickerDialog was just an example, and get back to the core of the question.
Your compiler told you that the way you call UpdateData()
CWnd::UpdateData(FALSE);
is specifying a call to a static function UpdateData() of the class CWnd, and that doesn't exists.
You need to call that function on a specific instance of CWnd. Luckily - you have it! You pass your this pointer to that thread function:
AfxBeginThread(MyThreadProc,this);
So inside that function you can safely cast LPVOID Param (that you are not using currently) to a pointer to CWnd or CMultithreadDlg. Then you will be able to call any non-static functions via that pointer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vlad Feinstein |
