ITimedEvents.cs 2.4 KB

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