Skip to content

Instantly share code, notes, and snippets.

@joey-g
Created August 20, 2018 18:00
Show Gist options
  • Save joey-g/e65c6855efd47f56bad92a805142ab85 to your computer and use it in GitHub Desktop.
Save joey-g/e65c6855efd47f56bad92a805142ab85 to your computer and use it in GitHub Desktop.
WaitUntil.cs
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