• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2678
  • Last Modified:

Delay Function in Delphi

Is there a function available just like the Sleep(..) in C?
Besides Timer can we write a function to delay acurrately time?
0
ysimon
Asked:
ysimon
1 Solution
 
rwilson032697Commented:
Use sleep!

Cheers,

Raymond.
0
 
philipleighsCommented:
ie. Sleep is a Win32 API function, not a C language function.
0
 
hagurCommented:
Hello,

Use this procedure, it works just like the delay function in Pascal.

procedure TForm1.Delay(msecs:integer);
var
   FirstTickCount:longint;
begin
     FirstTickCount:=GetTickCount;
     repeat    
           Application.ProcessMessages; {allowing access to other
                                         controls, etc.}
     until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
drajaytandonCommented:
Note:

Becareful using the application.processmessages solution, as although it works, it uses 100% CPU load.
0
 
StuartB_99Commented:
You can use Sleep(), as it is in the Windows API, to use it you must include Windows to your uses clause then just simply type Sleep(n);

procedure Sleep; external kernel32 name 'Sleep';

procedure Sleep(dwMilliseconds: DWORD); stdcall;

eg

Procedure TForm1.WaitFor(Seconds : Integer);
Begin
 Sleep(Seconds);
End;
0
 
stevenvhCommented:
I don't agree with drajaytandon's remark about application.processmessages using 100% CPU time. This method lets Windoze check the message loop for waiting messages from other apps. In fact if you perform actions which take a long time to finish, and you don't execute application.processmessages from time to time, other apps may appear to be frozen, notwithstanding Microcrap's claims about preemptive multitasking.
0
 
drajaytandonCommented:
Stevenvh's comment is true, we need to use Application.processmessages to allow the message queue to be processed but in NT you see that 100% CPU load is being used.

I have heard a combination of Application.ProcessMessages and Sleep (1) should be used, with Sleep (1) being used sparingly.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now