Last active
January 22, 2017 14:20
-
-
Save jpluimers/09e8b24818633045b4b0a02714266095 to your computer and use it in GitHub Desktop.
An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough. (updated thanks to Anders Melander)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TryGetLocationLockWithExponentialBackOff: Boolean; | |
const | |
cBase = 2; | |
cMaxWaitMilliSeconds = 1500; | |
var | |
lWaitMilliSeconds: integer; | |
begin | |
Result := True; | |
lWaitMilliSeconds := cBase; | |
while (not TryGetLocationLock) do | |
begin | |
if (lWaitMilliSeconds > cMaxWaitMilliSeconds) then | |
Exit(False); | |
Sleep(lWaitMilliSeconds); | |
Inc(lWaitMilliSeconds, lWaitMilliSeconds); | |
end; | |
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TryGetLocationLockWithExponentialBackOff: Boolean; | |
const | |
cBase = 2; | |
cMaxWaitMilliSeconds = 1500; | |
var | |
lIteration: Integer; | |
lWaitMilliSeconds: Single; | |
begin | |
lIteration := 1; | |
lWaitMilliSeconds := 0; | |
while lWaitMilliSeconds < cMaxWaitMilliSeconds do | |
begin | |
Result := TryGetLocationLock; | |
if Result then | |
Exit; | |
lWaitMilliSeconds := IntPower(cBase, lIteration); | |
Inc(lIteration); | |
Sleep(Round(lWaitMilliSeconds)); | |
end; | |
Result := TryGetLocationLock; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment