/// <summary> /// Starts a new timer. /// </summary> /// <param name="action"> /// The method to call on timer tick. If the method returns false, the timer will stop. /// </param> /// <param name="interval">The interval at which to tick.</param> /// <param name="priority">The priority to use.</param> /// <returns>An <see cref="IDisposable"/> used to cancel the timer.</returns> public static IDisposable Run(Func<bool> action, TimeSpan interval, DispatcherPriority priority = DispatcherPriority.Normal) { var timer = new DispatcherTimer(priority); timer.Interval = interval; timer.Tick += (s, e) => { if (!action()) { timer.Stop(); } }; timer.Start(); return Disposable.Create(() => timer.Stop()); }
/// <summary> /// Runs a method once, after the specified interval. /// </summary> /// <param name="action"> /// The method to call after the interval has elapsed. /// </param> /// <param name="interval">The interval after which to call the method.</param> /// <param name="priority">The priority to use.</param> /// <returns>An <see cref="IDisposable"/> used to cancel the timer.</returns> public static IDisposable RunOnce( Action action, TimeSpan interval, DispatcherPriority priority = DispatcherPriority.Normal) { var timer = new DispatcherTimer(priority) { Interval = interval }; timer.Tick += (s, e) => { action(); timer.Stop(); }; timer.Start(); return Disposable.Create(() => timer.Stop()); }