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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job