Threads in Windows service
-
I have a Windows service in which I need to stop worker threads from my main thread. I am trying to use events to do this but I am stumped on why it is not working. Each class that runs a worker thread creates a stop event and a response event. When I need to stop the worker thread I call into the threads class and set the event. However, instead of the worker thread detecting that the event has been set and stopping gracefully, it just disappears. Why? What is the best method for stopping a worker thread from the main thread?
-
I have a Windows service in which I need to stop worker threads from my main thread. I am trying to use events to do this but I am stumped on why it is not working. Each class that runs a worker thread creates a stop event and a response event. When I need to stop the worker thread I call into the threads class and set the event. However, instead of the worker thread detecting that the event has been set and stopping gracefully, it just disappears. Why? What is the best method for stopping a worker thread from the main thread?
Seeing some code might help. Doesn't the event code execute in the calling thread? (I hadn't considered this until now.) I've done similar things: Calling the Thread's Abort method Setting a semaphore that the thread checks
-
I have a Windows service in which I need to stop worker threads from my main thread. I am trying to use events to do this but I am stumped on why it is not working. Each class that runs a worker thread creates a stop event and a response event. When I need to stop the worker thread I call into the threads class and set the event. However, instead of the worker thread detecting that the event has been set and stopping gracefully, it just disappears. Why? What is the best method for stopping a worker thread from the main thread?
What do you do in your worker thread? If you have something like a while(...) { do something } then you can loop until a signal (e.g. a AutoResetEvent) is set by the main thread. Urs
-^-^-^-^-^-^-^- no risk no funk
-
What do you do in your worker thread? If you have something like a while(...) { do something } then you can loop until a signal (e.g. a AutoResetEvent) is set by the main thread. Urs
-^-^-^-^-^-^-^- no risk no funk
void CFileChangeEvent::stopProcessThread() { TRACE( "> > > > CFileChangeEvent::stopProcessThread() entered - Event handles < %X, %X >, Event ID < %s >\n", m_hProcessStopEvent, m_hProcessRespEvent, getIdentifier() ); // The ProcessStopEvent is used by the main thread to tell the worker thread to stop. // In turn, the worker thread sets the ProcessRespEvent to tell the main thread that it has stopped. ::SetEvent( m_hProcessStopEvent ); } UINT CFileChangeEvent::workerThread( LPVOID lpParam ) { CFileChangeEvent* pEvent = (CFileChangeEvent*)lpParam; HANDLE hProcessStopEvent = pEvent->getProcessStopEvent(); for( ; iterFile != vecFifFiles.end() && ! bCancelled; iterFile++ ) { Do some work here DWORD dwWaitStatus = ::WaitForSingleObject( hProcessStopEvent, 1 ); if ( dwWaitStatus == WAIT_FAILED ) { TRACE( "> > > > CFileChangeEvent::workerThread() - ::WaitForSingleObject() failed - Event handle < %X >\n", hProcessStopEvent ); bCancelled = true; } else if ( dwWaitStatus == WAIT_OBJECT_0 ) bCancelled = true; } }
Thanks for your help. -
void CFileChangeEvent::stopProcessThread() { TRACE( "> > > > CFileChangeEvent::stopProcessThread() entered - Event handles < %X, %X >, Event ID < %s >\n", m_hProcessStopEvent, m_hProcessRespEvent, getIdentifier() ); // The ProcessStopEvent is used by the main thread to tell the worker thread to stop. // In turn, the worker thread sets the ProcessRespEvent to tell the main thread that it has stopped. ::SetEvent( m_hProcessStopEvent ); } UINT CFileChangeEvent::workerThread( LPVOID lpParam ) { CFileChangeEvent* pEvent = (CFileChangeEvent*)lpParam; HANDLE hProcessStopEvent = pEvent->getProcessStopEvent(); for( ; iterFile != vecFifFiles.end() && ! bCancelled; iterFile++ ) { Do some work here DWORD dwWaitStatus = ::WaitForSingleObject( hProcessStopEvent, 1 ); if ( dwWaitStatus == WAIT_FAILED ) { TRACE( "> > > > CFileChangeEvent::workerThread() - ::WaitForSingleObject() failed - Event handle < %X >\n", hProcessStopEvent ); bCancelled = true; } else if ( dwWaitStatus == WAIT_OBJECT_0 ) bCancelled = true; } }
Thanks for your help. -
void CFileChangeEvent::stopProcessThread() { TRACE( "> > > > CFileChangeEvent::stopProcessThread() entered - Event handles < %X, %X >, Event ID < %s >\n", m_hProcessStopEvent, m_hProcessRespEvent, getIdentifier() ); // The ProcessStopEvent is used by the main thread to tell the worker thread to stop. // In turn, the worker thread sets the ProcessRespEvent to tell the main thread that it has stopped. ::SetEvent( m_hProcessStopEvent ); } UINT CFileChangeEvent::workerThread( LPVOID lpParam ) { CFileChangeEvent* pEvent = (CFileChangeEvent*)lpParam; HANDLE hProcessStopEvent = pEvent->getProcessStopEvent(); for( ; iterFile != vecFifFiles.end() && ! bCancelled; iterFile++ ) { Do some work here DWORD dwWaitStatus = ::WaitForSingleObject( hProcessStopEvent, 1 ); if ( dwWaitStatus == WAIT_FAILED ) { TRACE( "> > > > CFileChangeEvent::workerThread() - ::WaitForSingleObject() failed - Event handle < %X >\n", hProcessStopEvent ); bCancelled = true; } else if ( dwWaitStatus == WAIT_OBJECT_0 ) bCancelled = true; } }
Thanks for your help. -
I am wondering if the WaitForSingleObject() doesn't work if the event gets set before WaitForSingleObject() is called. It seems that it would just return the correct status of the event object.