Skip to content

Instantly share code, notes, and snippets.

@jeremydmiller
Created February 14, 2012 15:23
Show Gist options
  • Save jeremydmiller/1827533 to your computer and use it in GitHub Desktop.
Save jeremydmiller/1827533 to your computer and use it in GitHub Desktop.
Polling for a condition -- only to be used in automated testing code
public static class Wait
{
public static void Until(Func<bool> condition, int millisecondPolling = 500, int timeoutInMilliseconds = 5000)
{
if (condition()) return;
var reset = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(o =>
{
while (true)
{
try
{
if (condition()) break;
}
catch (Exception)
{
// Yeah, that's right. I wanna swallow these exceptions
}
Thread.Sleep(100);
}
reset.Set();
});
reset.WaitOne(timeoutInMilliseconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment