Adding method for waiting, with timeout, until a condition is true

This commit is contained in:
Chris Cameron
2017-11-15 10:54:35 -05:00
parent c6aa7d7b3e
commit 3322b2038f
2 changed files with 51 additions and 4 deletions

View File

@@ -12,6 +12,28 @@ namespace ICD.Common.Utils
{
public static class ThreadingUtils
{
/// <summary>
/// Wait until the given condition is true.
/// </summary>
/// <param name="condition"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public static bool Wait(Func<bool> condition, long timeout)
{
if (condition == null)
throw new ArgumentNullException("condition");
DateTime end = IcdEnvironment.GetLocalTime().AddMilliseconds(timeout);
while (!condition())
{
if (IcdEnvironment.GetLocalTime() >= end)
return false;
}
return true;
}
/// <summary>
/// Puts the current thread to sleep for the given amount of time.
/// </summary>