public void Timer_SetNegativeInterval_IsRejected() { Exception ex = null; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { try { using (Timer timer = new Timer()) { timer.Interval = TimeSpan.FromMilliseconds(-1); } } catch (Exception error) { ex = error; } }); } Assert.IsNotNull(ex, "Timer.Interval should reject negative intervals"); }
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 SingleShotTimer_Elapsed_CanRestartTimer() { int actionCount = 0; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { if (actionCount == 0) { timer.Restart(); } ++actionCount; }; timer.AutoReset = false; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.AreEqual(2, actionCount, "Timer did not honor Restart when called from 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_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 Timer_SetTooBigInterval_IsRejected() { Exception ex = null; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { try { using (Timer timer = new Timer()) { timer.Interval = TimeSpan.FromMilliseconds(((double)int.MaxValue) + 1); } } catch (Exception error) { ex = error; } }); } Assert.IsNotNull(ex, "Timer.Interval should reject intervals larger than int.MaxValue"); }
public void Constructor_SyncContextNotSupportingSynchronized_ThrowsInvalidOperationException() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null)) { using (Timer timer = new Timer()) { } } }
public void Timer_AfterConstruction_IsDisabled() { bool enabled = true; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { enabled = timer.Enabled; } }); } Assert.IsFalse(enabled, "Timer.Enabled should initially be false"); }
static void Main(string[] args) { nito.Start(); nito.DoSynchronously(() => { timer = new Nito.Async.Timer(); timer.AutoReset = false; timer.Elapsed += timer_Elapsed; Session.OnAudioDataArrived += Session_OnAudioDataArrived; Session.OnAudioStreamComplete += Session_OnAudioStreamComplete; Session.OnNotifyMainThread += Session_OnNotifyMainThread; try { Session.appkey = System.IO.File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "spotify_appkey.key")); } catch { } bool gui = (args == null || args.Length == 0); Log.gui = gui; if (Session.appkey == null || Session.appkey.Length == 0) { Log.Error("Error: Can't find app key file spotify_appkey.key!"); } else { lame.LameInit(); if (gui) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frm = new Form1(Session.Login()); } else { DoConsole(args); } } }); if (Log.gui) { Application.Run(frm); } }
public void TimerType_AfterSetSingleShot_IsSingleShot() { bool autoReset = true; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetSingleShot(TimeSpan.FromMilliseconds(50)); autoReset = timer.AutoReset; } }); } Assert.IsFalse(autoReset, "Timer should be single-shot"); }
public void TimerType_AfterSetPeriodic_IsPeriodic() { bool autoReset = false; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetPeriodic(TimeSpan.FromMilliseconds(50)); autoReset = timer.AutoReset; } }); } Assert.IsTrue(autoReset, "Timer should be periodic"); }
public void Timer_AfterSetSingleShot_IsEnabled() { bool enabled = false; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetSingleShot(TimeSpan.FromMilliseconds(50)); enabled = timer.Enabled; } }); } Assert.IsTrue(enabled, "Timer.Enabled should be true"); }
public void TimerInterval_AfterSetSingleShot_IsSetToArgument() { TimeSpan interval = default(TimeSpan); using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetSingleShot(TimeSpan.FromMilliseconds(50)); interval = timer.Interval; } }); } Assert.AreEqual(TimeSpan.FromMilliseconds(50), interval, "Timer should have its Interval set"); }
public void Timer_Running_CanChangeInterval() { TimeSpan interval = default(TimeSpan); using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.SetSingleShot(TimeSpan.FromMilliseconds(0)); timer.Interval = TimeSpan.FromMilliseconds(1); interval = timer.Interval; } }); } Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be settable while the timer is running"); }
public void Timer_ElapsedAfterDisposed_DoesNotInvokeElapsed() { bool sawAction = false; using (ActionThread thread = new ActionThread()) { thread.Start(); thread.DoSynchronously(() => { using (Timer timer = new Timer()) { timer.Elapsed += () => { sawAction = true; }; timer.SetSingleShot(TimeSpan.FromMilliseconds(0)); Thread.Sleep(10); } }); } Assert.IsFalse(sawAction, "Disposed timer invoked 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"); }
public void PeriodicTimer_Elapsed_InvokesElapsedMoreThanOnce() { int actionCount = 0; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { ++actionCount; }; timer.AutoReset = true; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.IsTrue(actionCount > 1, "Timer did not run Elapsed more than once"); }
public void SingleShotTimer_Elapsed_InvokesElapsedExactlyOnce() { int actionCount = 0; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { ++actionCount; }; timer.AutoReset = false; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.AreEqual(1, actionCount, "Timer did not run Elapsed exactly once"); }
// This is the first action done by the main loop static void FirstAction() { // Create the timer Timer timer = new Timer(); // Set the Elapsed handler timer.Elapsed += timer_Elapsed; // Start the timer timer.SetSingleShot(TimeSpan.FromMilliseconds(1)); Thread.Sleep(10); // Dispose the timer timer.Dispose(); // Note that the elapsed method will *not* be called // even though the timer has elapsed, because it was // Disposed before returning to the main loop // Exit the main loop ActionDispatcher.Current.QueueExit(); }
public void SingleShotTimer_Elapsed_IsDisabled() { bool enabled = true; using (ActionThread thread = new ActionThread()) { thread.Start(); Timer timer = null; thread.DoSynchronously(() => { timer = new Timer(); timer.Elapsed += () => { enabled = timer.Enabled; }; timer.AutoReset = false; timer.Interval = TimeSpan.FromMilliseconds(0); timer.Enabled = true; }); Thread.Sleep(10); thread.DoSynchronously(() => timer.Dispose()); } Assert.IsFalse(enabled, "Single-shot Timer should be disabled when called from Elapsed"); }
static void Main(string[] args) { nito.Start(); nito.DoSynchronously(() => { timer = new Nito.Async.Timer(); timer.AutoReset = false; timer.Elapsed += timer_Elapsed; Session.OnAudioDataArrived += Session_OnAudioDataArrived; Session.OnAudioStreamComplete += Session_OnAudioStreamComplete; Session.OnNotifyMainThread += Session_OnNotifyMainThread; try { Session.appkey = System.IO.File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "spotify_appkey.key")); } catch { } bool gui = (args == null || args.Length == 0); Log.gui = gui; if (Session.appkey == null || Session.appkey.Length == 0) { Log.Error("Error: Can't find app key file spotify_appkey.key!"); } else { lame.LameInit(); if (gui) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frm = new Form1(Session.Login()); } else { DoConsole(args); } } }); if (Log.gui) Application.Run(frm); }