How to get time difference in Inno Setup?

2019-02-19 04:08发布

I want to write a while loop with a timeout like follows... How to write this in Inno Setup?

InitialTime = SystemCurrentTime ();

Timeout = 2000; //(ms)

while (!condition) {    
    if (SystemCurrentTime () - InitialTime > Timeout) {
    // Timed out
       break;
    }
}

Thanks!

标签: inno-setup
1条回答
可以哭但决不认输i
2楼-- · 2019-02-19 05:08

To make it simple in Inno Setup, you can use GetTickCount calls.

The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds.

So, it will not timeout exactly at 2000 milliseconds (or whatever value you want) but close enough to be acceptable.

Other limitation you must be aware of is:

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

In code, it shows like this:

[Code]
function GetTickCount: DWord; external 'GetTickCount@kernel32 stdcall';

procedure WaitForTheCondition;
const 
  TimeOut = 2000;
var
  InitialTime, CurrentTime: DWord;
begin
  InitialTime := GetTickCount;
  while not Condition do
  begin
    CurrentTime := GetTickCount;
    if    ((CurrentTime - InitialTime) >= TimeOut) { timed out OR }
       or (CurrentTime < InitialTime) then { the rare case of the installer running  }
                                           { exactly as the counter overflows, }
      Break;
  end;
end;

The above function is not perfect, in the rare case of that being running at the moment the counter overflows (once each 49.7 days of the machine is continuously running), because it will timeout as soon as the overflow occurs (maybe before the desired wait).

查看更多
登录 后发表回答