diff --git a/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs b/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs new file mode 100644 index 000000000..cfb2b3ecd --- /dev/null +++ b/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs @@ -0,0 +1,31 @@ +using System; +using Terminal.Gui; +using static Terminal.Gui.MainLoop; + +/// +/// for timeout events (e.g. ) +/// +public class TimeoutEventArgs : EventArgs { + /// + /// Gets the timeout callback handler + /// + public Timeout Timeout { get; } + + /// + /// Gets the in UTC time when the + /// will next execute after. + /// + public long Ticks { get; } + + + /// + /// Creates a new instance of the class. + /// + /// + /// + public TimeoutEventArgs (Timeout timeout, long ticks) + { + this.Timeout = timeout; + this.Ticks = ticks; + } +} \ No newline at end of file diff --git a/Terminal.Gui/Core/MainLoop.cs b/Terminal.Gui/Core/MainLoop.cs index 4ec3824f6..b48fe2a77 100644 --- a/Terminal.Gui/Core/MainLoop.cs +++ b/Terminal.Gui/Core/MainLoop.cs @@ -97,7 +97,7 @@ namespace Terminal.Gui { /// Invoked when a new timeout is added. To be used in the case /// when is . /// - public event Action TimeoutAdded; + public event EventHandler TimeoutAdded; /// /// Creates a new Mainloop. @@ -161,7 +161,7 @@ namespace Terminal.Gui { lock (timeoutsLockToken) { var k = (DateTime.UtcNow + time).Ticks; timeouts.Add (NudgeToUniqueKey (k), timeout); - TimeoutAdded?.Invoke (k); + TimeoutAdded?.Invoke (this, new TimeoutEventArgs(timeout, k)); } } diff --git a/UnitTests/Application/MainLoopTests.cs b/UnitTests/Application/MainLoopTests.cs index 46a0a9b32..b88ffbb41 100644 --- a/UnitTests/Application/MainLoopTests.cs +++ b/UnitTests/Application/MainLoopTests.cs @@ -264,6 +264,35 @@ namespace Terminal.Gui.ApplicationTests { Assert.False (ml.RemoveTimeout (token)); } + // Timeout Handler Tests + [Fact] + public void AddTimer_EventFired () + { + var ml = new MainLoop (new FakeMainLoop ()); + var ms = 100; + + var originTicks = DateTime.UtcNow.Ticks; + + var callbackCount = 0; + Func callback = (loop) => { + callbackCount++; + return true; + }; + + object sender = null; + TimeoutEventArgs args = null; + ml.TimeoutAdded += (s, e) => { + sender = s; + args = e; + }; + + var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback); + + Assert.Same (ml,sender); + Assert.NotNull (args.Timeout); + Assert.True (args.Ticks - originTicks >= 100 * TimeSpan.TicksPerMillisecond); + + } [Fact] public void AddTimer_Run_Called () {