stopping a code for 2 seconds and resume

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsn
    New Member
    • Sep 2007
    • 237

    stopping a code for 2 seconds and resume

    hello everyone.
    my question is simple. how can i make my code while it is executing to stop for 2 or 3 seconds and then resume executing.

    thanks in advance
    hsn
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Thread.sleep. .

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      Originally posted by r035198x
      is there any other way rather than using threads??

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by hsn
        is there any other way rather than using threads??
        You can never run away from threads. There is always at least one thread executing anyway. What's wrong with using Thread.sleep? It's very easy to use too ...

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by hsn
          is there any other way rather than using threads??
          You could save the time with System.currentM illies() (if I recall correctly) in a long variable when you start and then run a loop which checks, if the 2 seconds have passed yet over and over again. If so, the loop ends. This is not a nice solution and will use a great deal of processing power, but it will work without the obvious mentioning of Threads.

          Of course, you could always just use sleep as suggested by r0.

          Greetings,
          Nepomuk

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            Originally posted by hsn
            is there any other way rather than using threads??
            there are a lot of other ways possible, but maybe you don't like them either:

            1.) run your code in a debugger and then stop it, wait for 3 seconds and then resume.
            2.) produce a hardware failure which freezes your whole Windows for a few seconds. A good way to do this is to use wrong network card or USB drivers. I like hardware interrupts!
            3.) make a second program and let it run for 3 seconds with higher priority than your old program. Hint: If you don't have the time to program (you could just use the nonsense loop mentioned earlier in this forum topic), just let some existing viruses run and do the job for you.
            You also could synchronize with Hotmail in your Outlook to freeze your computer for some time.
            4.) Accelerate your computer to light speed for 3 seconds.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by chaarmann
              ...
              4.) Accelerate your computer to light speed for 3 seconds.
              * giggles *

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by chaarmann
                1.) run your code in a debugger and then stop it, wait for 3 seconds and then resume.
                Ah, but the OP want's the code to pause for 2 seconds, not 3. So that is obviously no option. ;-)
                Originally posted by chaarmann
                2.) produce a hardware failure which freezes your whole Windows for a few seconds. A good way to do this is to use wrong network card or USB drivers. I like hardware interrupts!
                Remember, that the OP want's the code to resume - you never know, if Windows will do that. Plus, it wouldn't be platform independent. :-D
                Originally posted by chaarmann
                3.) make a second program and let it run for 3 seconds with higher priority than your old program. Hint: If you don't have the time to program (you could just use the nonsense loop mentioned earlier in this forum topic), just let some existing viruses run and do the job for you.
                You also could synchronize with Hotmail in your Outlook to freeze your computer for some time.
                Using a program not written yourself can be be very unreliable in such cases - just imagine, the virus could have unwanted side effects! (Well, so could Outlook - you could download Spam!)
                Originally posted by chaarmann
                4.) Accelerate your computer to light speed for 3 seconds.
                I'm assuming, that the energy consumption would be slightly of the limits. Oh, and in the process of accelerating the computer to the speed of light, the pressure would become so high, that the computer would easily be squeezed into a strange unidentifiable piece of mass. Therefore, the calculation would probably not continue after deceleration.

                So probably, Thread.sleep is the most viable option. ;-)

                Greetings,
                Nepomuk

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Note that the Thread.sleep() method has nothing much to do with threads; it just
                  happens to be a static method in that class.

                  kind regards,

                  Jos

                  Comment

                  • hsn
                    New Member
                    • Sep 2007
                    • 237

                    #10
                    thanks guys for your help

                    regards
                    hsn

                    Comment

                    • tharden3
                      Contributor
                      • Jul 2008
                      • 916

                      #11
                      did you try asking it nicely? Playing easy listening music?

                      Comment

                      • chaarmann
                        Recognized Expert Contributor
                        • Nov 2007
                        • 785

                        #12
                        Originally posted by nepomuk
                        Ah, but the OP want's the code to pause for 2 seconds, not 3. So that is obviously no option. ;-)
                        If you want it so accurate, then I must admit that it is nearly impossible to stop for 2 seconds exactly at all. The timer inside the computer is not precise enough compared to an atomic clock. And even if you increase the precision of your clock more and more, you will never hit 2 seconds exactly. But there is an infinitesimal chance that if you stop, you really could do it after exactly 2 seconds. But you never know afterwards if you did it or not (Greetings from Heisenberg, uncertainty principle).
                        Originally posted by nepomuk
                        Remember, that the OP want's the code to resume - you never know, if Windows will do that. Plus, it wouldn't be platform independent. :-D
                        I can give him my old USB stick. If you stick that into your PC, it will automatically install a driver that will freeze your computer for roughly 2 seconds whenever you stick in any other USB stick afterwards. It is proven and some "friends" who always borrow USB sticks instead of buying their own can prove that fact (and remember it well, because they had to reinstall WIndows to get rid of the delay).
                        And I still remember the extra hardware with which you could freeze a C64 while running a game. Then you could save its whole memory on a floppy. (which was very convenient to bypass the copy protection).
                        Originally posted by nepomuk
                        Using a program not written yourself can be be very unreliable in such cases - just imagine, the virus could have unwanted side effects! (Well, so could Outlook - you could download Spam!)
                        Well, it's easy if you have Windows 95. It's very reliable there. If you ever watched movies on your computer and suddenly your movie keeps on freezing for a half a second after every 3 seconds or so, then you know you caught one again. Watching movies there was the best and reliable virus scanner I ever had.
                        Always think positive, turn unwanted side effects to your advantage. It's not a bug, it's a feature!
                        Originally posted by nepomuk
                        I'm assuming, that the energy consumption would be slightly of the limits. Oh, and in the process of accelerating the computer to the speed of light, the pressure would become so high, that the computer would easily be squeezed into a strange unidentifiable piece of mass. Therefore, the calculation would probably not continue after deceleration.
                        You are right.So it's better to keep your computer at low speed and turn back your computer clock afterwards manually, but don't tell anyone, (This is what I always do if the evaluation period of some software ran out)
                        Originally posted by nepomuk
                        So probably, Thread.sleep is the most viable option. ;-)

                        Greetings,
                        Nepomuk
                        Speaking of sleep, my wife just called me the third time to go to bed . So if your computer evolves in a future day and starts marrying a female one, then going to sleep for some time cannot be denied anymore by any thread.

                        Good night,
                        Chris

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          Originally posted by chaarmann
                          If you want it so accurate, then I must admit that it is nearly impossible to stop for 2 seconds exactly at all. [...] But you never know afterwards if you did it or not (Greetings from Heisenberg, uncertainty principle).
                          Damn. Well, at least nobody else will be able to prove, that it wasn't exactly 2 seconds. ;-)
                          Originally posted by chaarmann
                          I can give him my old USB stick. If you stick that into your PC, it will automatically install a driver that will freeze your computer for roughly 2 seconds whenever you stick in any other USB stick afterwards.
                          Doesn't solve the "platform independence issue" however! :-D
                          Originally posted by chaarmann
                          And I still remember the extra hardware with which you could freeze a C64 while running a game. Then you could save its whole memory on a floppy. (which was very convenient to bypass the copy protection).
                          Very, very naughty! ;-)
                          Originally posted by chaarmann
                          Well, it's easy if you have Windows 95. [...] Watching movies there was the best and reliable virus scanner I ever had.
                          :-D Hopefully however, the OP isn't using Windows 95.
                          Originally posted by chaarmann
                          Always think positive, turn unwanted side effects to your advantage. It's not a bug, it's a feature!
                          Well, it is a feature - a feature, the programmer of the virus probably intended. But you don't like all features of all programs, do you?
                          Originally posted by chaarmann
                          You are right.So it's better to keep your computer at low speed and turn back your computer clock afterwards manually, but don't tell anyone, (This is what I always do if the evaluation period of some software ran out)
                          Again: Naughty, naughty!
                          Originally posted by chaarmann
                          Speaking of sleep, my wife just called me the third time to go to bed . So if your computer evolves in a future day and starts marrying a female one, then going to sleep for some time cannot be denied anymore by any thread.
                          I hear, Windows 7 should have this feature! ;-)

                          Greetings,
                          Nepomuk

                          Comment

                          Working...