Question

I am creating a procedure that I need to run then wait 10 minutes before moving on

procedure firstTimeRun(const document: IHTMLDocument2);
var
  fieldValue : string;
  StartTime : Dword;
begin
  StartTime := GetTickCount();
  WebFormSetFieldValue(document, 0, 'Username', '*******');
  WebFormSetFieldValue(document, 0, 'Password', '*******');
  WebFormSubmit(document, 0);
 if (GetTickCount() >= StartTime+ 600000) then
 begin
   SecondRun();
 end; 
 end; 

the problem I have is when I get to the if statement it will check see that it is not true and move on how do I make it stay and wait until the statement is true?

Was it helpful?

Solution

Naively the answer is that you need a while loop:

while GetTickCount() < StartTime+600000 then
  ;
SecondRun();

Or perhaps more easily read, a repeat loop:

repeat
until GetTickCount() >= StartTime+600000;
SecondRun();

But that's the wrong way to do it. You'll run the processor hot for 10 minutes, and achieve nothing. And I'm glossing over the fact that if your system has been up for 49 days then you'll hit GetTickCount wrap-around and the logic of the test is then flawed.

The operating system has a function designed to solve your problem, it is called Sleep.

Sleep(600000);

This blocks the calling thread for the specified number of milliseconds. Because the thread is blocks, the thread will not consume CPU resource while it is waiting.

This will make the calling thread unresponsive, so it's typically something that you would do in a background thread rather than an application's main thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top