public void PeriodicTimer_Elapsed_CanChangeToSingleShot() { bool autoReset = true; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { timer.SetSingleShot(timer.Interval); autoReset = timer.AutoReset; timer.Cancel(); }; timer.AutoReset = true; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.IsFalse(autoReset, "Periodic Timer should be able to change to Single-shot within Elapsed"); }
public void PeriodicTimer_Elapsed_IsEnabled() { bool enabled = false; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { enabled = timer.Enabled; timer.Cancel(); }; timer.AutoReset = true; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.IsTrue(enabled, "Periodic Timer should be enabled when called from Elapsed"); }
public void PeriodicTimer_Elapsed_CanChangeInterval() { TimeSpan interval = default(TimeSpan); using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { timer.Interval = TimeSpan.FromMilliseconds(1); interval = timer.Interval; timer.Cancel(); }; timer.AutoReset = true; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be honored when called from Elapsed"); }
public void PeriodicTimer_Elapsed_CanCancelTimer() { int actionCount = 0; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { ++actionCount; timer.Cancel(); }; timer.AutoReset = true; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.AreEqual(1, actionCount, "Timer did not honor Cancel when called from Elapsed"); }
public void Timer_AfterCancel_IsDisabled() { bool enabled = true; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetPeriodic(TimeSpan.FromMilliseconds(50)); timer.Cancel(); enabled = timer.Enabled; } }); } Assert.IsFalse(enabled, "Timer.Enabled should be false"); }