Question : Passing CDIalog to a thread

What is the best and the safest way to pass a CDialog object to a thread and still be able to call functions like UpdateData() ?
So far, I pass a pointer MyDialog* m as an LPVOID parameter of my worker thread (I use AfxBeginThread). I also pass HWND handle m_Hwnd, and instead
m->UpdateData(FALSE);
which causes a bunch of ASSERTs, use
FromHandle(m_Hwnd)->UpdateData(FALSE);
 but I am not sure what actually happens to the temporary pointer FromHandle(m_Hwnd), and if MFC takes any care of cleaning it up.

Answer : Passing CDIalog to a thread

I had problems like you and I handeled them similar like:

In OninitDialog, post youself a UserMessage like
PostMessage(WM_DLG_LOGIN_CALLBACK, CREATE_THREAD);

Handle this message like:
....
      ON_MESSAGE(WM_DLG_LOGIN_CALLBACK, OnDoCallback)
....

LRESULT CDlgXX::OnDoCallback(WPARAM wParam, LPARAM lParam)
{
switch( wParam )
{
case CREATE_THREAD:
 m_pThreadDlg = AfxBeginThread (ThreadDlgLogin, m_hWnd, THREAD_PRIORITY_NORMAL, 0, 0);
break;
case DO_UPDATE:
  UpdateData(lParam);
  break;


....

Your thread-function looks like
UINT ThreadDlgLogin(LPVOID pParam)
{
CWnd *pDlg = CWnd::FromHandle((HWND) pParam);


 To notify your dialog to do s.th, use:
if( pDlg && IsWindow(pDlg->m_hWnd) ) pDlg->PostMessage(WM_DLG_LOGIN_CALLBACK, DO_UPDATE, FALSE);


In a header you have to define:

enum {
CREATE_THREAD,      
DO_UPDATE};


Random Solutions  
 
programming4us programming4us