Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Last active January 22, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpluimers/09e8b24818633045b4b0a02714266095 to your computer and use it in GitHub Desktop.
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)
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;
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