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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To stop some activity

To stop some activity

Scheduled Pinned Locked Moved C / C++ / MFC
windows-adminhelpcareer
10 Posts 5 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
    rajeevktripathi
    wrote on last edited by
    #1

    Hi All working on a dialog based application, where the entire registry is scaneed for errors, For Registry using SDK functions ( RegOpenKey(), RegEnumKey( ) etc.) Now what my problem is: while scanning is going on ( there is a button "STOP" on the dialog) if user press or click on the STOP button then the the scanning of Registry should be stopped and a messagebox should appear for resume or exit operation. I think problem would be clear... So please suggest me the scenario to implement this and better if provide some code snippet for the same. Thanks

    C O R 3 Replies Last reply
    0
    • R rajeevktripathi

      Hi All working on a dialog based application, where the entire registry is scaneed for errors, For Registry using SDK functions ( RegOpenKey(), RegEnumKey( ) etc.) Now what my problem is: while scanning is going on ( there is a button "STOP" on the dialog) if user press or click on the STOP button then the the scanning of Registry should be stopped and a messagebox should appear for resume or exit operation. I think problem would be clear... So please suggest me the scenario to implement this and better if provide some code snippet for the same. Thanks

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Start the scanning of the resgistry in a separate thread that runs an "infinite loop". This loop can be finished either when the scan is complete or when a variable is set to false. Something like that (pseudo-code):

      while (bContinue)
      {
      // Process next key
      if (LastKey == true)
      bContinue = false;
      }

      This variable can be set through the main thread but you have to make it thread safe (for example use a critical section). If you are new to thread, I suggest you read some documentation about that because it can be difficult for a beginner.


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • R rajeevktripathi

        Hi All working on a dialog based application, where the entire registry is scaneed for errors, For Registry using SDK functions ( RegOpenKey(), RegEnumKey( ) etc.) Now what my problem is: while scanning is going on ( there is a button "STOP" on the dialog) if user press or click on the STOP button then the the scanning of Registry should be stopped and a messagebox should appear for resume or exit operation. I think problem would be clear... So please suggest me the scenario to implement this and better if provide some code snippet for the same. Thanks

        O Offline
        O Offline
        ovidiucucu
        wrote on last edited by
        #3

        First, I would like to recommend instead a message box, two buttons "Pause" and "Stop" in your main dialog. Perform the registry scanning in a worker thread. When press "Pause", call SuspendThread dunction in order to suspend registry scanning and change the button caption from "Pause" to "Resume". When the user press "Resume", call ResumeThread to start scanning again. When the user press "Stop" you can call TerminateThread. Ovidiu Microsoft MVP - Visual C++ @to Cédric: Why "infinite loop"? -- modified at 10:09 Friday 23rd June, 2006

        C D 2 Replies Last reply
        0
        • O ovidiucucu

          First, I would like to recommend instead a message box, two buttons "Pause" and "Stop" in your main dialog. Perform the registry scanning in a worker thread. When press "Pause", call SuspendThread dunction in order to suspend registry scanning and change the button caption from "Pause" to "Resume". When the user press "Resume", call ResumeThread to start scanning again. When the user press "Stop" you can call TerminateThread. Ovidiu Microsoft MVP - Visual C++ @to Cédric: Why "infinite loop"? -- modified at 10:09 Friday 23rd June, 2006

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          ovidiucucu wrote:

          Why "infinite loop"?

          I prefer using a loop that you can fully control instead of killing the thread with terminate thread. The reason is if you want to make some 'clean-up' after the loop (ok, maybe infinite loop was not the right word but I meant a loop that finishes when one of the condition is met).


          Cédric Moonen Software developer
          Charting control

          O 1 Reply Last reply
          0
          • O ovidiucucu

            First, I would like to recommend instead a message box, two buttons "Pause" and "Stop" in your main dialog. Perform the registry scanning in a worker thread. When press "Pause", call SuspendThread dunction in order to suspend registry scanning and change the button caption from "Pause" to "Resume". When the user press "Resume", call ResumeThread to start scanning again. When the user press "Stop" you can call TerminateThread. Ovidiu Microsoft MVP - Visual C++ @to Cédric: Why "infinite loop"? -- modified at 10:09 Friday 23rd June, 2006

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

            Using SuspendThread() and TerminateThread() is very risky, and not nearly as simple as you've described here. Before a thread suspends itself, it needs to ensure that by being suspended, the calling thread is not affected. If a synchronization object is being held by the thread, it would need to be released. Suspending a thread from another, without taking all of the precautions, is not a good idea because the thread being suspended might be in the middle of something important (e.g., memory allocation). This could result in your application entering a "hung" state. If a thread calls TerminateThread() on itself, there is no chance for cleanup. The thread goes away right then and there.


            "The largest fire starts but with the smallest spark." - David Crow

            "Judge not by the eye but by the heart." - Native American Proverb

            O 1 Reply Last reply
            0
            • C Cedric Moonen

              ovidiucucu wrote:

              Why "infinite loop"?

              I prefer using a loop that you can fully control instead of killing the thread with terminate thread. The reason is if you want to make some 'clean-up' after the loop (ok, maybe infinite loop was not the right word but I meant a loop that finishes when one of the condition is met).


              Cédric Moonen Software developer
              Charting control

              O Offline
              O Offline
              ovidiucucu
              wrote on last edited by
              #6

              You are double-right. First when say "maybe infinite loop was not the right" because registry is a finite stuff and sooner or later its scanning have to finish, regardless the user pushes "Stop" or not. ;-) Second, indeed for the sake of good recommendations we have to avoid TerminateThread, which in our case can let a registry key open for reading which can lead in unpredictable, catastrophic effects. :) Ovidiu Microsoft MVP -- modified at 10:25 Friday 23rd June, 2006

              1 Reply Last reply
              0
              • D David Crow

                Using SuspendThread() and TerminateThread() is very risky, and not nearly as simple as you've described here. Before a thread suspends itself, it needs to ensure that by being suspended, the calling thread is not affected. If a synchronization object is being held by the thread, it would need to be released. Suspending a thread from another, without taking all of the precautions, is not a good idea because the thread being suspended might be in the middle of something important (e.g., memory allocation). This could result in your application entering a "hung" state. If a thread calls TerminateThread() on itself, there is no chance for cleanup. The thread goes away right then and there.


                "The largest fire starts but with the smallest spark." - David Crow

                "Judge not by the eye but by the heart." - Native American Proverb

                O Offline
                O Offline
                ovidiucucu
                wrote on last edited by
                #7

                So, instead of SuspendThread/ResumeThread the right solution is... Ovidiu Microsoft MVP

                D J 2 Replies Last reply
                0
                • O ovidiucucu

                  So, instead of SuspendThread/ResumeThread the right solution is... Ovidiu Microsoft MVP

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

                  I didn't indicate they couldn't be used. You have to be very careful when using them, however.


                  "The largest fire starts but with the smallest spark." - David Crow

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • O ovidiucucu

                    So, instead of SuspendThread/ResumeThread the right solution is... Ovidiu Microsoft MVP

                    J Offline
                    J Offline
                    Jun Du
                    wrote on last edited by
                    #9

                    Avoid TerminateThread(). A thread synchronization should be implemented to allow the thread function to complete/end gracefully. Best, Jun

                    1 Reply Last reply
                    0
                    • R rajeevktripathi

                      Hi All working on a dialog based application, where the entire registry is scaneed for errors, For Registry using SDK functions ( RegOpenKey(), RegEnumKey( ) etc.) Now what my problem is: while scanning is going on ( there is a button "STOP" on the dialog) if user press or click on the STOP button then the the scanning of Registry should be stopped and a messagebox should appear for resume or exit operation. I think problem would be clear... So please suggest me the scenario to implement this and better if provide some code snippet for the same. Thanks

                      R Offline
                      R Offline
                      rajeevktripathi
                      wrote on last edited by
                      #10

                      Thanks u all Infact I am new to threading programming. Though have some theortical basics of the concept. So u r requested to tell me the link to begin with so that things could be more clear. 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