Remotely/Shared/Utilities/TaskHelper.cs
2021-07-29 07:56:55 -07:00

34 lines
923 B
C#

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Remotely.Shared.Utilities
{
public static class TaskHelper
{
public static bool DelayUntil(Func<bool> condition, TimeSpan timeout, int pollingMs = 10)
{
var sw = Stopwatch.StartNew();
while (!condition() && sw.Elapsed < timeout)
{
Thread.Sleep(pollingMs);
}
return condition();
}
public static Task<bool> DelayUntilAsync(Func<bool> condition, TimeSpan timeout, int pollingMs = 10)
{
return Task.Run(() =>
{
var sw = Stopwatch.StartNew();
while (!condition() && sw.Elapsed < timeout)
{
Thread.Sleep(pollingMs);
}
return condition();
});
}
}
}