Example #1
0
        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");
        }
Example #2
0
        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");
        }
Example #3
0
        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");
        }
Example #4
0
        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");
        }
Example #5
0
        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");
        }
Example #6
0
        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");
        }
Example #7
0
        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");
        }
Example #8
0
        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");
        }
Example #9
0
        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");
        }
Example #10
0
 public void Constructor_SyncContextNotSupportingSynchronized_ThrowsInvalidOperationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
     {
         using (Timer timer = new Timer())
         {
         }
     }
 }
Example #11
0
 public void Constructor_SyncContextNotSupportingSynchronized_ThrowsInvalidOperationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
     {
         using (Timer timer = new Timer())
         {
         }
     }
 }
Example #12
0
        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");
        }
Example #13
0
        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);
            }
        }
Example #14
0
        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");
        }
Example #15
0
        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");
        }
Example #16
0
        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");
        }
Example #17
0
        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");
        }
Example #18
0
        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");
        }
Example #19
0
        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");
        }
Example #20
0
        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");
        }
Example #21
0
        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");
        }
Example #22
0
        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();
    }
Example #24
0
        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");
        }
Example #25
0
        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");
        }
Example #26
0
        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");
        }
Example #27
0
        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");
        }
Example #28
0
        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");
        }
Example #29
0
        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");
        }
Example #30
0
        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");
        }
Example #31
0
        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");
        }
Example #32
0
        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");
        }
Example #33
0
        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");
        }
Example #34
0
        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");
        }
Example #35
0
        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");
        }
Example #36
0
        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);
        }
Example #37
0
        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");
        }