title: Thread Fails to Exit On Application Exit - C++
body: <p>My application creates a thread that polls for Windows messages.  When it is time to close down, my application sends the <code>WM_QUIT</code> message.</p>
  <p>In the application thread, this is how I am attempting to shut things down:</p>
  <pre><code>if ( _hNotifyWindowThread != NULL )
  {
      ASSERT(_pobjNotifyWindow != NULL);
  
      ::SendMessage( _pobjNotifyWindow-&gt;m_hWnd, WM_QUIT, 0, 0 );
      ::WaitForSingleObject( _hNotifyWindowThread, 50000L );
      ::CloseHandle( _hNotifyWindowThread ); // &lt;-- PC never gets here.
      _hNotifyWindowThread = NULL;
  }
  </code></pre>
  <p>This is the message pump running in my thread function:</p>
  <pre><code>// Start the message pump...
  while ( (bRetVal = ::GetMessage(
      &amp;msg,                           // message structure
      _pobjNotifyWindow-&gt;m_hWnd,      // handle to window whose messages are to be retrieved
      WM_DEVICECHANGE,                // lowest message value to retrieve
      WM_DEVICECHANGE                 // highest message value to retrieve
      )) != 0 )
  {
      switch ( bRetVal )
      {
      case -1:                        // Error generated in GetMessage.
          TRACE(_T(&quot;NotifyWindowThreadFn : Failed to get notify window message.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n&quot;), ::GetLastError(), __WFILE__, __LINE__);
          return ::GetLastError();
          break;
  
      default:                        // Other message received.
          ::TranslateMessage( &amp;msg );
          ::DispatchMessage( &amp;msg );
          break;
      }
  }
  
  delete _pobjNotifyWindow;           // Delete the notify window.
  
  return msg.wParam;                  // Return exit code.
  </code></pre>
  <p>The <a href="http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx" rel="nofollow noreferrer">Microsoft documentation for <code>GetMessage</code></a> states:</p>
  <blockquote>
  <p>If the function retrieves the WM_QUIT message, the return value is zero.</p>
  <p>Note that GetMessage always retrieves WM_QUIT messages, no matter which values you specify for wMsgFilterMin and wMsgFilterMax.</p>
  </blockquote>
  <p>If this is the case, then I would expect a call to <code>GetMessage</code> that retrieves the <code>WM_QUIT</code> message to return 0.  However, debugging leaves me to believe that the message is not received properly.  What is odd is that I can place a breakpoint in the <code>WndProc</code> function, and it seems to get the <code>WM_QUIT</code> message.</p>
  <p>What am I doing wrong?  Should I be using a different function for posting messages between threads?  Thanks.</p>