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();
        break;

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

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.

Answers


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

replace

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

with

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

replace

 ( (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

Transforming a rectangle in Python

python numpy matplotlib transform

I have any transformation matrix, for example:

Write/Read a memory-protected process's memory

c++ windows memory token

I'll try to explain my problem the best I can.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.