Jump to content

Issues with Sleep(1) called in a loop

Recommended Posts

Posted (edited)

I use a thread to update an animated 'please wait' dialog while doing other things in the background.


The issue is, I want to be able to dismiss the please wait dialog instantly and since I use animations, I use "Sleep" commands to to time the animation and take minimum CPU time.


However, for some reason, doing Sleep(10) returns a vastly different result than calling For I := 0 to 1 do If Terminated = False then Sleep(5); which for some reasons takes ~30ms to complete.

For reference, calling For I := 0 to 9 do If Terminated = False then Sleep(1); takes ~150ms.


My problem is with Delphi 7, but I doubt it's specific to this version of Delphi, any ideas?

Edited by Yaron

Share this post

Link to post

Nevermind, I replaced Sleep with WaitForSingleObject and used an event to break out of the wait when closing the dialog.

Share this post

Link to post
14 hours ago, Yaron said:

any ideas?

Answer can be found in the documentation of Sleep. Sleep can only wait for multiples of system ticks. And your system appears to tick at a frequency of 15ms. Which is typical, that being the default tick frequency. There are many many discussions of this online. Here's one: https://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution

  • Like 2

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now