using System;
using ICD.Common.Properties;
using ICD.Common.Utils.Services;
using ICD.Common.Utils.Services.Logging;
#if SIMPLSHARP
using Crestron.SimplSharp;
#else
using System.Threading.Tasks;
#endif
namespace ICD.Common.Utils
{
public static class ThreadingUtils
{
///
/// Wait until the given condition is true.
///
///
///
/// False if the call times out
public static bool Wait(Func 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;
}
///
/// Puts the current thread to sleep for the given amount of time.
///
///
public static void Sleep(int milliseconds)
{
#if SIMPLSHARP
CrestronEnvironment.Sleep(milliseconds);
#else
Task.Delay(TimeSpan.FromMilliseconds(milliseconds)).Wait();
#endif
}
///
/// Executes the callback as a short-lived, threaded task.
///
///
[PublicAPI]
public static object SafeInvoke(Action callback)
{
return SafeInvoke