Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threads in Windows service

Threads in Windows service

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RoyceF
    wrote on last edited by
    #1

    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?

    P U 2 Replies Last reply
    0
    • R RoyceF

      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?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R RoyceF

        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?

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • U Urs Enzler

          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

          R Offline
          R Offline
          RoyceF
          wrote on last edited by
          #4

          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.

          G R 2 Replies Last reply
          0
          • R RoyceF

            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.

            G Offline
            G Offline
            Gareth H
            wrote on last edited by
            #5

            RoyceF, You seem to be in the wrong forum. Regards, Gareth.

            R 1 Reply Last reply
            0
            • R RoyceF

              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.

              R Offline
              R Offline
              RoyceF
              wrote on last edited by
              #6

              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.

              R 1 Reply Last reply
              0
              • G Gareth H

                RoyceF, You seem to be in the wrong forum. Regards, Gareth.

                R Offline
                R Offline
                RoyceF
                wrote on last edited by
                #7

                Sorry, I thought I was in the C++/MFC forum.

                1 Reply Last reply
                0
                • R RoyceF

                  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.

                  R Offline
                  R Offline
                  RoyceF
                  wrote on last edited by
                  #8

                  I have re-posted this in the C++/MFC forum. I would move it if I could. Thanks.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups