Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How do I make my Delphi programs go to sleep?

How do I make my Delphi programs go to sleep?


Note: Since the following was written, Delphi 2.0 has added a Sleep command. The following code can be used in Delphi 2.0 (though you'd have to rename it), but it's necessary only in Delphi 1.0.

One thing Delphi does not have is a sleep function, which causes a program to wait a number of seconds before proceeding to its next task. In the meantime, it will allow Windows background operations to process.

The Turbo Pascal equivalent to this is delay, but unfortunately I haven't been able to find any documentation on it, so I don't know if it is still available. It should be, but to be safe, I came up with a new function called Sleep.

The procedure makes use of the Windows API function GetTickCount, which returns the number of milliseconds that have elapsed since Windows has started. The WinAPI documentation states that after 49 days of continuous use, the tick count recycles back to zero, so if your machine runs on a continuous basis with Windows, you may consider this.

Here's the code:


This code operates on a simple principle: While (Time B - Time A)
< SleepLength

 then process background operations.

procedure Sleep(SleepSecs : Integer);
  StartValue : LongInt;

  {Initialize vars}
  StartValue    := GetTickCount; {Get value of current milliseconds elapsed}
  While ((GetTickCount - StartValue) <= (SleepSecs * 1000)) do

Although this code is essentially only two lines long, you can implement forced delays in your programs to accommodate other processes running in the background.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date