ITimedEvents.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. namespace Terminal.Gui.App;
  2. /// <summary>
  3. /// Manages timers.
  4. /// </summary>
  5. public interface ITimedEvents
  6. {
  7. /// <summary>
  8. /// Adds a timeout to the application.
  9. /// </summary>
  10. /// <remarks>
  11. /// When the specified time passes, the callback will be invoked. If the callback returns <see langword="true"/>, the
  12. /// timeout will be
  13. /// reset, repeating the invocation. If it returns <see langword="false"/>, the timeout will stop and be removed. The
  14. /// returned value is a
  15. /// token that can be used to stop the timeout by calling <see cref="Remove"/>.
  16. /// </remarks>
  17. object Add (TimeSpan time, Func<bool> callback);
  18. /// <inheritdoc cref="Add(System.TimeSpan,System.Func{bool})"/>
  19. object Add (Timeout timeout);
  20. /// <summary>
  21. /// Invoked when a new timeout is added. To be used in the case when
  22. /// <see cref="IApplication.StopAfterFirstIteration"/> is <see langword="true"/>.
  23. /// </summary>
  24. event EventHandler<TimeoutEventArgs>? Added;
  25. /// <summary>
  26. /// Removes a previously scheduled timeout.
  27. /// </summary>
  28. /// <remarks>
  29. /// The token parameter is the value returned by <see cref="Add(TimeSpan, Func{bool})"/> or <see cref="Add(Timeout)"/>.
  30. /// </remarks>
  31. /// <returns>
  32. /// <see langword="true"/> if the timeout is successfully removed; otherwise, <see langword="false"/>.
  33. /// This method also returns <see langword="false"/> if the timeout is not found.
  34. /// </returns>
  35. bool Remove (object token);
  36. /// <summary>
  37. /// Runs all timeouts that are due.
  38. /// </summary>
  39. void RunTimers ();
  40. /// <summary>
  41. /// Returns the next planned execution time (key - UTC ticks)
  42. /// for each timeout that is not actively executing.
  43. /// </summary>
  44. SortedList<long, Timeout> Timeouts { get; }
  45. /// <summary>
  46. /// Gets the timeout for the specified event.
  47. /// </summary>
  48. /// <param name="token">The token of the event.</param>
  49. /// <returns>The <see cref="TimeSpan"/> for the event, or <see lang="null"/> if the event is not found.</returns>
  50. TimeSpan? GetTimeout (object token);
  51. /// <summary>Stops and removes all timed events.</summary>
  52. void StopAll ();
  53. }