I will probably need this again somewhere in the future: An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough.
It’s Delphi, but I’ve not seen practical implementations in C# either.
(the updated version thanks to Anders Melander).
–jeroen
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; |