Created
August 20, 2018 18:00
-
-
Save joey-g/e65c6855efd47f56bad92a805142ab85 to your computer and use it in GitHub Desktop.
WaitUntil.cs
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
private void WaitUntil(Action assertion, int timeoutMillis) | |
{ | |
var cancelTokenSource = new CancellationTokenSource(timeoutMillis); | |
var token = cancelTokenSource.Token; | |
var t = Task.Factory.StartNew(() => | |
{ | |
while (true) | |
{ | |
try | |
{ | |
token.ThrowIfCancellationRequested(); | |
assertion(); | |
break; | |
} | |
catch (OperationCanceledException e) | |
{ | |
throw new OperationCanceledException("Timed out while waiting on assertion."); | |
} | |
catch (Exception) | |
{ | |
// Allow retry attempts to fire | |
} | |
} | |
}, token); | |
Task.WaitAll(t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment