How do I make my Delphi programs go to sleep?

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 nexttask. In the meantime, it will allow Windows background operations to process.

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

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

Here’sthe code:

{====================================================================================This code operates on a simple principle: While (Time B - Time A)< SleepLength then process background operations. ====================================================================================}procedure Sleep(SleepSecs : Integer);var  StartValue : LongInt;begin  {Initialize vars}  StartValue    := GetTickCount; {Get value of current milliseconds elapsed}  While ((GetTickCount - StartValue) <= (SleepSecs * 1000)) do    Application.ProcessMessages;end;

Although this code is essentially only two lines long, you canimplement forced delays in your programsto accommodate other processes running in the background.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several