title: Deadlock with a thread shut down
body: <p>I am writing a dialog based multithreading application with Visual C++. It has several views. </p>
  
  <p>OnInitialUpdate function I start a working thread like this:</p>
  
  <pre><code>pThread = AfxBeginThread(MultiThread, this, THREAD_PRIOIRITY_NORMAL, 0 CREATE_SUSPENDED, NULL);
  pThread-&gt;m_bAutoDelete = false;
  pThread-&gt;ResumeThread();
  </code></pre>
  
  <p>OnDestroy function I have</p>
  
  <pre><code>eventKill.SetEvent();
  WaitForSingleObject(pHread-&gt;m_hThread, INFINITE);
  delete pThread
  </code></pre>
  
  <p>MultiThread function has </p>
  
  <pre><code>...
  while (WaitForSingleObject(pView-&gt;eventKill.m_hObject, 0) != WAIT_OBJECT_0)
  {
      ...
      pView-&gt;InvalidateRect(rect);
      pView-&gt;UpdateWindow();
      Sleep(100);
  }
  return 0;
  </code></pre>
  
  <p>This usually shuts down nicely, but I have found one exception. I have this thread and its view running. There is another view which is currently on top and the thread view is partially under it. If I close the thread view by pressing directly its close button without first making this thread view the topmost window, the program deadlocks in WaitForSingleObject(pThread->m_hThread, INFINITE). If I do not use UpdateWindow there will be no deadlocks. If I make the thread view the topmost before pressing the close button, there are no problems either. So why the deadlock?</p>