multithreading - Thread Fails to Exit On Application Exit - C++ -


my application creates thread polls windows messages. when time close down, application sends wm_quit message.

in application thread, how attempting 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 message pump running in thread function:

// start message pump... while ( (bretval = ::getmessage(     &msg,                           // message structure     _pobjnotifywindow->m_hwnd,      // handle window messages retrieved     wm_devicechange,                // lowest message value retrieve     wm_devicechange                 // highest message value retrieve     )) != 0 ) {     switch ( bretval )     {     case -1:                        // error generated in getmessage.         trace(_t("notifywindowthreadfn : failed 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 notify window.  return msg.wparam;                  // return exit code. 

the microsoft documentation getmessage states:

if function retrieves wm_quit message, return value zero.

note getmessage retrieves wm_quit messages, no matter values specify wmsgfiltermin , wmsgfiltermax.

if case, expect call getmessage retrieves wm_quit message return 0. however, debugging leaves me believe message not received properly. odd can place breakpoint in wndproc function, , seems wm_quit message.

what doing wrong? should using different function posting messages between threads? thanks.

this complete answer (i'm 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 windowsprocedure :

 case wm_close : destroywindow( hwnd ); break; //can return    case wm_destroy : postquitmessage( 0 );  

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -