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
This file contains hidden or 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 hidden or 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; |





