Search code examples
c#wait

How do I wait for a delegate to return a certain value?


I am currently using Selenium WebDriverWait to wait for things to happend where I don't need the IWebDriver functionality. My code looks like this:

public static T WaitForNotNull<T>(this IWebDriver driver, Func<T> func)
{
    var result = default(T);

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(d => (result = func()) != null);

    return result;
}

public static void WaitForNull<T>(this IWebDriver driver, Func<T> func)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(d => func() == null);
}

Is there a similar construct in .Net which I can use instead of WebDriverWait?


Solution

  • The answer is

    No

    There is no such thing in the .NET Framework, you will have to write such a method yourself.