ITimedEvents.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #nullable enable
  2. using System.Collections.ObjectModel;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Manages timers and idles
  6. /// </summary>
  7. public interface ITimedEvents
  8. {
  9. /// <summary>
  10. /// Adds specified idle handler function to main iteration processing. The handler function will be called
  11. /// once per iteration of the main loop after other events have been handled.
  12. /// </summary>
  13. /// <param name="idleHandler"></param>
  14. void AddIdle (Func<bool> idleHandler);
  15. /// <summary>
  16. /// Runs all idle hooks
  17. /// </summary>
  18. void LockAndRunIdles ();
  19. /// <summary>
  20. /// Runs all timeouts that are due
  21. /// </summary>
  22. void LockAndRunTimers ();
  23. /// <summary>
  24. /// Called from <see cref="IMainLoopDriver.EventsPending"/> to check if there are any outstanding timers or idle
  25. /// handlers.
  26. /// </summary>
  27. /// <param name="waitTimeout">
  28. /// Returns the number of milliseconds remaining in the current timer (if any). Will be -1 if
  29. /// there are no active timers.
  30. /// </param>
  31. /// <returns><see langword="true"/> if there is a timer or idle handler active.</returns>
  32. bool CheckTimersAndIdleHandlers (out int waitTimeout);
  33. /// <summary>Adds a timeout to the application.</summary>
  34. /// <remarks>
  35. /// When time specified passes, the callback will be invoked. If the callback returns true, the timeout will be
  36. /// reset, repeating the invocation. If it returns false, the timeout will stop and be removed. The returned value is a
  37. /// token that can be used to stop the timeout by calling <see cref="RemoveTimeout(object)"/>.
  38. /// </remarks>
  39. object AddTimeout (TimeSpan time, Func<bool> callback);
  40. /// <summary>Removes a previously scheduled timeout</summary>
  41. /// <remarks>The token parameter is the value returned by AddTimeout.</remarks>
  42. /// <returns>
  43. /// Returns
  44. /// <see langword="true"/>
  45. /// if the timeout is successfully removed; otherwise,
  46. /// <see langword="false"/>
  47. /// .
  48. /// This method also returns
  49. /// <see langword="false"/>
  50. /// if the timeout is not found.
  51. /// </returns>
  52. bool RemoveTimeout (object token);
  53. /// <summary>
  54. /// Returns all currently registered idles. May not include
  55. /// actively executing idles.
  56. /// </summary>
  57. ReadOnlyCollection<Func<bool>> IdleHandlers { get;}
  58. /// <summary>
  59. /// Returns the next planned execution time (key - UTC ticks)
  60. /// for each timeout that is not actively executing.
  61. /// </summary>
  62. SortedList<long, Timeout> Timeouts { get; }
  63. /// <summary>Removes an idle handler added with <see cref="AddIdle(Func{bool})"/> from processing.</summary>
  64. /// <returns>
  65. /// <see langword="true"/>
  66. /// if the idle handler is successfully removed; otherwise,
  67. /// <see langword="false"/>
  68. /// .
  69. /// This method also returns
  70. /// <see langword="false"/>
  71. /// if the idle handler is not found.</returns>
  72. bool RemoveIdle (Func<bool> fnTrue);
  73. /// <summary>
  74. /// Invoked when a new timeout is added. To be used in the case when
  75. /// <see cref="Application.EndAfterFirstIteration"/> is <see langword="true"/>.
  76. /// </summary>
  77. event EventHandler<TimeoutEventArgs>? TimeoutAdded;
  78. }