My Program Runs Too Fast - But Sleep Is Too Slow
A loop in my program runs too fast. But if I add "Sleep 1" it runs too slowly. I can't put "Sleep 0.2" because that rounds down to 0, and acts like Sleep isn't even there. It must get rounded, because "Sleep .49" is too fast, and "Sleep .51" is too slow.
[264 byte] By [
jalak7] at [2007-11-20 10:14:54]

# 1 Re: My Program Runs Too Fast - But Sleep Is Too Slow
You are doing it wrong:
Simulate CPU Based Task
Specifies a CPU task as part of the test.
Sleep Period (ms)
Specifies the sleep period in milliseconds for the CPU task.
+/-
Specifies the ms variation using Sleep Period (ms) as a base for the CPU tasks sleep period.
Task Duration (ms)
Specifies the duration in ms for each CPU task. Due to the possibility of multi-tasking other non-APE application tasks, this value is not the same as specifying that each CPU task should be fully active for the defined duration. Instead, each task will start and stop according to the specified duration, using the CPU resources as available during that time.
+/-
Specifies the ms variation using Task Duration (ms) as a base for the CPU task.
SLEEP 3000 ' 3 seconds
# 3 Re: My Program Runs Too Fast - But Sleep Is Too Slow
Hi,
Try DoEvents instead of Sleep...
You can use multiple DoEvents...
DoEvents: DoEvents
.
What would that do? (Besides really mess up the program)
The problem is that sleep wasn't slowing down the program?
# 5 Re: My Program Runs Too Fast - But Sleep Is Too Slow
No, you'd use DoEvents every 100 records or so when doing an update of 10000 records. That's just to allow the loop to let other processes that may be running to 'catch up' to the system.
example: updating a display, or when the user clicks a button.
# 6 Re: My Program Runs Too Fast - But Sleep Is Too Slow
A loop in my program runs too fast. But if I add "Sleep 1" it runs too slowly. I can't put "Sleep 0.2" because that rounds down to 0, and acts like Sleep isn't even there. It must get rounded, because "Sleep .49" is too fast, and "Sleep .51" is too slow.
What does this loop do? Can you show us the code? Are you doing some kind of animation or something?
# 7 Re: My Program Runs Too Fast - But Sleep Is Too Slow
Well can I add my 2c ...
With out knowing what your 'Super Fast' loop is doing it's a bit difficult to give ideas And if Adding Sleep 1 makes it too slow, then it seems you need somewhat finer control..
This is a tried and tested method for speed control that i use..
Add a timer to the Form..
At it's fastest a timer will tick once every 15-16ms,
For each tick of the timer you can do one iterration of your loop, Or even two or more if its still too slow... By adjusting the Timer you can speed up and slow down your loop..
The problems with Sleep and DoEvents..
Sleep: The proccesor essentialy stops for the specified time. Using 100% of the allocated time slice, doing NOTHING..
DoEvents: The Delay depends on the amount of proc. time requested by other applications.. No time Requested = No delay.. Hence the Speed of the loop relys on the amount of proc time used by other applications.. If you have another app running that needs alot of proc time, your loop slows down alot..
Sleep and Doevents do have there place in a program, but not in the case of slowing down loops...
Best case usage for sleep and DoEvents..
Sleep: When using external ports to communicate. IE. Serial/ Parallel ports. Where you need to stop the proc for a moment so that the external device has time to respond to data, but you do not want to give up the time slice to another application.
Doevents: When you have a loop that may need to be stoped at the users request. Placing a DoEvents near the end of the loop (Just before Next, Loop or Wend) So that the loop has completed the iteration and before is starts with the next you can pause/ Exit or Alter the options. Also used for GUI Status Updates.
Using a Timer you have a loop that will run at a set speed no mater what Proc it's running on or what other app's are calling for proc time...
For a full working example on using timers to create effects have a look at the moving Images thread and also the Crop/Zoom image project (http://www.dev-archive.com/forum/showthread.php?p=1519506#post1519506) where timers are used to flash a selection box, Smooth slide a image, and Smooth Zoom the Image..
If you give us an idea of what your loop does we can put together a example to demonstrate ..
Gremmy...