473,390 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

stopping a code for 2 seconds and resume

hsn
237 100+
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
Jul 23 '08 #1
12 7306
r035198x
13,262 8TB
Thread.sleep. .
Jul 23 '08 #2
hsn
237 100+
Thread.sleep. .
is there any other way rather than using threads??
Jul 23 '08 #3
r035198x
13,262 8TB
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 ...
Jul 23 '08 #4
Nepomuk
3,112 Expert 2GB
is there any other way rather than using threads??
You could save the time with System.currentMillies() (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
Jul 23 '08 #5
chaarmann
785 Expert 512MB
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.
Jul 23 '08 #6
r035198x
13,262 8TB
...
4.) Accelerate your computer to light speed for 3 seconds.
* giggles *
Jul 23 '08 #7
Nepomuk
3,112 Expert 2GB
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. ;-)
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
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!)
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
Jul 23 '08 #8
JosAH
11,448 Expert 8TB
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
Jul 23 '08 #9
hsn
237 100+
thanks guys for your help

regards
hsn
Jul 23 '08 #10
tharden3
916 512MB
did you try asking it nicely? Playing easy listening music?
Jul 23 '08 #11
chaarmann
785 Expert 512MB
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).
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).
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!
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)
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
Jul 23 '08 #12
Nepomuk
3,112 Expert 2GB
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. ;-)
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
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! ;-)
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.
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?
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!
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
Jul 24 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Keith Langer | last post by:
I have an application which consists of a main work thread and multiple threads which each maintain a TCP socket. When a configuration change occurs, all activity on the socket threads must be...
9
by: Joe Rigley | last post by:
Hello, As a .NET newbie I'd appreciate some advice... I've been tasked with writing a small app (in VB .NET) that takes some data from our HR system, does some format modifications, and loads...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
8
by: darnnews | last post by:
I have a form. When a person selects a publication from a listbox, this snippet of code is supposed to look up authors that correspond to that publication and populate the Author List Box, but...
1
by: mclaugb | last post by:
Here is a simple piece of thread code. When i add the print (or other) function into the run method--the thread fails to stop after 2 seconds which the join(2) should ensure. I have a function...
4
by: bjm | last post by:
I am writing a program that will automate a series of application installations. I want to give the user the option of stopping the program's execution in between installations (for example, give...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
10
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace...
0
by: zhaira | last post by:
i have this code downloaded from other site, but im not sure how to create a Pause and Resume function for this timer all i have is the code for pause. tmrWait.enabled= false ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.