winapi - C++ thread termination with waiting window -


so, code goes somehow this:

main(){ /*waiting window class declaration*/     threadinfo* othread=new threadinfo(); //an object me know when finish thread     queueuserworkitem((lpthread_start_routine)waitingwindow, (void*)mthread, wt_executelongfunction);     function_that_takes_time();     othread->setterminated(); //set member terminated bool true /*continue other things*/ } 

and waitingwindow function run on thread

msg msg; hwndwaiting=createwindow(...) // here window created while (msg.message != wm_quit)     {         if (peekmessage(&msg, null, 0u, 0u, pm_remove))         {             translatemessage(&msg);             dispatchmessage(&msg);         }         else         {             if(othread->isterminated()) // isterminated returns bool true if terminated             {                 delete othread;                 exitthread(0);             }         }     } exitthread(0); 

is exitthread way remove waiting window, , safely remove thread? (at least i'm 100% sure way when end it).

i'm asking because works nice in windows xp, crash "the application has stopped working" on windows 7.

thanks help.

the best way end threads in general, let them "gracefully" finish themselves. tell thread end setting event, example:

handle hevent_die = createevent(...);   handle hthread_something = createthread(...); // or _beginthread() ...  dword winapi thread_func (lpvoid param) {   while(working && waitforsingleobject(hevent_die, 0)!=wait_object_0)   {     ...   }    return 0; }   while (msg.message != wm_quit) {    ...     if(waitforsingleobject(hthread_something, 0) == wait_object_0)    {      // things if needed    } }  setevent(hevent_die); waitforsingleobject(hthread_something, infinite);  closehandle(hthread_something); closehandle(hevent_die); hthread_something = 0; hevent_die = 0; 

if using nested loops inside thread function, have end if receive event.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -