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 / C++ / MFC
  4. Check if a thread is still running

Check if a thread is still running

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 4 Posters 0 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.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    Hello, if I use AfxBeginThread() function to start a thread, how do I check if it's still running or has already exited?

    D M 2 Replies Last reply
    0
    • B Budric B

      Hello, if I use AfxBeginThread() function to start a thread, how do I check if it's still running or has already exited?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Normally you shouldn't care. What exactly is it that you are attempting to do?


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      B 1 Reply Last reply
      0
      • D David Crow

        Normally you shouldn't care. What exactly is it that you are attempting to do?


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        B Offline
        B Offline
        Budric B
        wrote on last edited by
        #3

        I have a thread that plays a sequence of images. In the CDocument I handle controls to pause/play using events and boolean variables. I chose start the thread inside play() function. The way I handle pause() I don't care if the thread is there or not. However if the user presses play again, I don't want to start another thread, I want to continue the previous one. I have the option of making a thread that runs for the life of the CDocument...but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.

        B D J 3 Replies Last reply
        0
        • B Budric B

          Hello, if I use AfxBeginThread() function to start a thread, how do I check if it's still running or has already exited?

          M Offline
          M Offline
          Marc Soleda
          wrote on last edited by
          #4

          AfxBeginThread returns the pointer of the created thread CWinThread. Using BOOL GetExitCodeThread( HANDLE hThread, LPDWORD lpExitCode); you can get the status of this thread (STILL_ACTIVE if it's not terminated). Marc Soleda ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

          1 Reply Last reply
          0
          • B Budric B

            I have a thread that plays a sequence of images. In the CDocument I handle controls to pause/play using events and boolean variables. I chose start the thread inside play() function. The way I handle pause() I don't care if the thread is there or not. However if the user presses play again, I don't want to start another thread, I want to continue the previous one. I have the option of making a thread that runs for the life of the CDocument...but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.

            B Offline
            B Offline
            Budric B
            wrote on last edited by
            #5

            Also I should clarify currently the thread goes through the sequence of images and quits. So next time inside play() I need to know whether to resume the current thread or start a new one.

            1 Reply Last reply
            0
            • B Budric B

              I have a thread that plays a sequence of images. In the CDocument I handle controls to pause/play using events and boolean variables. I chose start the thread inside play() function. The way I handle pause() I don't care if the thread is there or not. However if the user presses play again, I don't want to start another thread, I want to continue the previous one. I have the option of making a thread that runs for the life of the CDocument...but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              See here for details on how to properly pause and stop threads.


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

              B 1 Reply Last reply
              0
              • D David Crow

                See here for details on how to properly pause and stop threads.


                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                B Offline
                B Offline
                Budric B
                wrote on last edited by
                #7

                Yes, that's what I use. Taken from the site: // thread body: while(running) { /* loop */ if(paused) switch(::WaitForSingleObject(event, time)) { case WAIT_OBJECT_0: break; case WAIT_TIMEOUT: continue; } // rest of thread } /* loop */ except instead of while(running), I have a for loop from 0 to numImages.

                1 Reply Last reply
                0
                • B Budric B

                  I have a thread that plays a sequence of images. In the CDocument I handle controls to pause/play using events and boolean variables. I chose start the thread inside play() function. The way I handle pause() I don't care if the thread is there or not. However if the user presses play again, I don't want to start another thread, I want to continue the previous one. I have the option of making a thread that runs for the life of the CDocument...but wouldn't that be wasteful when it sits there and waits on pause forever after a single play.

                  J Offline
                  J Offline
                  Jim Crafton
                  wrote on last edited by
                  #8

                  Budric B. wrote: but wouldn't that be wasteful when it sits there and waits on pause forever after a single play. Not neccessarily. Or at least I wouldn't think so if you're using WaitForSingleObject() to do the "waiting" and not a busy wait while loop. ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF!

                  D 1 Reply Last reply
                  0
                  • J Jim Crafton

                    Budric B. wrote: but wouldn't that be wasteful when it sits there and waits on pause forever after a single play. Not neccessarily. Or at least I wouldn't think so if you're using WaitForSingleObject() to do the "waiting" and not a busy wait while loop. ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF!

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Jim Crafton wrote: ...if you're using WaitForSingleObject() to do the "waiting" and not a busy wait while loop. Correct. WaitForSingleObject() uses no CPU time while waiting for the object state to become signaled or the time-out interval to elapse.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    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