Thread Fails to Exit On Application Exit - C++

My application creates a thread that polls for Windows messages. When it is time to close down, my application sends the WM_QUIT message.

In the application thread, this is how I am attempting to shut things down:

if ( _hNotifyWindowThread != NULL )
    ASSERT(_pobjNotifyWindow != NULL);

    ::SendMessage( _pobjNotifyWindow->m_hWnd, WM_QUIT, 0, 0 );
    ::WaitForSingleObject( _hNotifyWindowThread, 50000L );
    ::CloseHandle( _hNotifyWindowThread ); // <-- PC never gets here.
    _hNotifyWindowThread = NULL;

This is the message pump running in my thread function:

// Start the message pump...
while ( (bRetVal = ::GetMessage(
    &msg,                           // message structure
    _pobjNotifyWindow->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("NotifyWindowThreadFn : Failed to get notify window message.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
        return ::GetLastError();

    default:                        // Other message received.
        ::TranslateMessage( &msg );
        ::DispatchMessage( &msg );

delete _pobjNotifyWindow;           // Delete the notify window.

return msg.wParam;                  // Return exit code.

The Microsoft documentation for GetMessage states:

If the function retrieves the WM_QUIT message, the return value is zero.

Note that GetMessage always retrieves WM_QUIT messages, no matter which values you specify for wMsgFilterMin and wMsgFilterMax.

If this is the case, then I would expect a call to GetMessage that retrieves the WM_QUIT 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 WndProc function, and it seems to get the WM_QUIT message.

What am I doing wrong? Should I be using a different function for posting messages between threads? Thanks.


This the complete answer (I'm almost sure):


::SendMessage( _pobjNotifyWindow->m_hWnd, WM_QUIT, 0, 0 ); 


::PostMessage( _pobjNotifyWindow->m_hWnd, WM_CLOSE, 0, 0 ); 


 ( (bRetVal = ::GetMessage( &msg, _pobjNotifyWindow->m_hWnd, WM_DEVICECHANGE, WM_DEVICECHANGE )) != 0 ) 

with ( (bRetVal = ::GetMessage( &msg, NULL ,0 ,0 )) != 0 )

In your WindowsProcedure :

 case WM_CLOSE : DestroyWindow( hWnd ); break; //can be return 

 case WM_DESTROY : PostQuitMessage( 0 ); 

Need Your Help

Selecting jQuery tabs from another DIV

jquery jquery-ui jquery-tabs

I am trying to position links to jQuery Tabs under the tab content, with the tabs navigation in its own DIV (for styling purposes). Here is my markup:

How can I fix a Flash security error #2048

flash security crossdomain.xml

I have having an error in flash #2048. Everything I can find points to the fact this this is a security precaution as the files are not on the same domain.