Created
February 14, 2012 15:23
-
-
Save jeremydmiller/1827533 to your computer and use it in GitHub Desktop.
Polling for a condition -- only to be used in automated testing code
This file contains 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
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