title: Send return value to dialog from thread MFC
body: <p>I'm creating dialog with do modal:</p>
  
  <pre><code>MainHamsterDlg MainHamsterDlg;
   if (MainHamsterDlg.DoModal() == IDCANCEL)
   break;
  </code></pre>
  
  <p>Then in that dialog create worker thread:</p>
  
  <pre><code>BOOL MainHamsterDlg::OnInitDialog()
      {
          AfxBeginThread(WorkerThreadProc, m_hWnd, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
          CDialogEx::OnInitDialog();
  
          return TRUE;
      }
  </code></pre>
  
  <p>the thread must exit dialog by sending return value IDCANCEL.</p>
  
  <pre><code>    UINT WorkerThreadProc(LPVOID Param) //Sample function for using in AfxBeginThread
      {
          Sleep(1000); // process simulation
          MainHamsterDlg * self = (MainHamsterDlg *)Param;
          self-&gt;EndDialog(IDCANCEL);
          return FALSE;
      }
  </code></pre>
  
  <p>When compiling I do not get any error. when processing then getting on the point:</p>
  
  <pre><code>self-&gt;EndDialog(IDCANCEL);
  </code></pre>
  
  <p>error message:</p>
  
  <pre><code>Unhandled exception at 0x01503AD4 in L2Hamster.exe: 0xC0000005:
  Access violation reading location 0x00000020.
  </code></pre>
  
  <p>that pointing my to dlgcore.cpp file:</p>
  
  <pre><code>void CDialog::EndDialog(int nResult)
  {
      ASSERT(::IsWindow(m_hWnd)); &lt;&lt;&lt;&lt;&lt;===== to that line
  
      m_bClosedByEndDialog = TRUE;
  
      if (m_nFlags &amp; (WF_MODALLOOP|WF_CONTINUEMODAL))
          EndModalLoop(nResult);
  
      ::EndDialog(m_hWnd, nResult);
  }
  </code></pre>
  
  <p>I don't know what I'm doing wrong. any solution?</p>