The rule is:
never use UI functions or manipulate UI objects in another thread.
If you want a thread to update an UI object, you can for example post or send a message: the message will be handled by the UI thread.
For example, let's say you created a thread inside a
CDialog
-derived class:
In h file:
class CYourDialog : public CDialog
{
...
static DWORD WINAPI YourThread(LPVOID param);
...
afx_msg LRESULT OnYourMessage(WPARAM wParam, LPARAM lParam);
};
In cpp file:
#define WM_YOUR_MESSAGE (WM_USER + x)
BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
....
ON_MESSAGE(WM_YOUR_MESSAGE, OnYourMessage)
END_MESSAGE_MAP()
DWORD WINAPI CYourDialog::YourThread(LPVOID param)
{
CYourDialog* pDlg = (CYourDialog*)param;
pDlg->SendMessage(WM_YOUR_MESSAGE, param1, param2);
...
return 0;
}
LRESULT CYourDialog::OnYourMessage(WPARAM wParam, LPARAM lParam)
{
return 0;
}