This topic is 4173 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I created a thread w/ THREAD_PRIORITY_TIME_CRITICAL. I also created an event using m_hStop = CreateEvent(NULL, TRUE, FALSE, NULL); I've noticed my thread does not always catch my event when I call SetEvent. Is this possible or should I look elsewhere for my problem? Thanks. I wait for my thread to exit like this: SetEvent(m_hStop); //wait for thread to exit while( (ExitCode == STILL_ACTIVE) && (NoError == TRUE) ) { Sleep(5); NoError = GetExitCodeThread(m_hMyThread, &ExitCode); } MyThread() { .... ..... while(bDone == false) { EventState = WaitForSingleObject(m_hStop, 0); switch(EventState) { case WAIT_TIMEOUT: //Do stuff .... break; case WAIT_OBJECT_0: bDone = true; //stop thread } } ExitThread(Stat); return(Stat); }

Share on other sites
Try setting the event and then waiting on the thread handle instead, e.g.
HANDLE hThread = CreateThread(...);// Do stuff...SetEvent(m_hStop);WaitForSingleObject(hThread, INFINITE);CloseHandle(hThread); // Always remember to close the thread handle!

Share on other sites
Are you sure the event is being set? If the thread running MyThread() is at time-critical (i.e., very high) priority, and never yeilds the CPU, your other thread may not get chance to run and set the event.

Share on other sites
Messing with thread priority often results in situations like this. Try a more friendly setting for your thread. In fact, it's more often the case that you'd want to set up LOWER priority threads than the parent, for housekeeping tasks, and leave the main thread with its default priority.