0

我正在创建一个需要运行的程序,然后等待 10 分钟再继续

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; 

我遇到的问题是,当我到达 if 语句时,它会检查它是否不正确,然后继续我如何让它保持并等到该语句为真?

4

1 回答 1

6

天真的答案是你需要一个while循环:

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

或者更容易阅读,一个repeat循环:

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

但这是错误的做法。您将让处理器热运行 10 分钟,但一无所获。而且我掩盖了这样一个事实,即如果您的系统已经运行了 49 天,那么您将遇到GetTickCount回绕,然后测试的逻辑就有缺陷。

操作系统具有旨在解决您的问题的功能,称为Sleep.

Sleep(600000);

这会阻塞调用线程指定的毫秒数。因为线程是块,所以线程在等待的时候不会消耗CPU资源。

这将使调用线程无响应,因此通常您会在后台线程而不是应用程序的主线程中执行此操作。

于 2013-12-13T16:18:42.387 回答