예제 #1
0
        public void InvalidOperationTests()
        {
            WatchdogTimer uut = new WatchdogTimer(int.MaxValue, "InvalidOperationTests");

            bool started = false;
            bool stopped = false;
            bool reset   = false;

            uut.OnStarted += () => { started = true; };
            uut.OnStopped += () => { stopped = true; };
            uut.OnReset   += () => { reset = true; };

            Assert.Throws <InvalidOperationException>(() => uut.Stop());  // Can't stop if its not started.
            Assert.Throws <InvalidOperationException>(() => uut.Reset());

            // Ensure events didn't fire.
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            // Call Start
            uut.Start();
            Assert.IsTrue(started);

            started = false;
            // Calling start again should get an InvalidOperationException
            Assert.Throws <InvalidOperationException>(() => uut.Start());
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            uut.Reset();
            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsTrue(reset);
            reset = false;

            uut.Stop();
            Assert.IsFalse(started);
            Assert.IsTrue(stopped);
            Assert.IsFalse(reset);
            stopped = false;

            uut.Dispose();

            // Should get ObjectDisposedExceptions
            Assert.Throws <ObjectDisposedException>(() => uut.Start());
            Assert.Throws <ObjectDisposedException>(() => uut.Stop());
            Assert.Throws <ObjectDisposedException>(() => uut.Reset());

            Assert.IsFalse(started);
            Assert.IsFalse(stopped);
            Assert.IsFalse(reset);

            // Nothing bad should happen if we call Dispose again
            uut.Dispose();
        }
예제 #2
0
 private void DestroyTimers()
 {
     if (_screenSaverTimer != null)
     {
         _screenSaverTimer.Stop();
     }
 }
예제 #3
0
        public void ResetTest()
        {
            using (WatchdogTimer uut = new WatchdogTimer(3000, "ResetTest"))
            {
                bool resetCalled = false;

                Exception err = new Exception("My Exception");

                uut.OnTimeoutExpired += delegate()
                {
                    resetCalled = true;
                };

                uut.Start();
                // Calling Reset every half second should
                // prevent the watchdog from firing, which is set to
                // expire after 3 seconds.
                for (int i = 0; i < 12; ++i)
                {
                    uut.Reset();
                    Thread.Sleep(500);
                }
                uut.Stop();

                Assert.IsFalse(resetCalled);
            }
        }
예제 #4
0
        public void StopEvent()
        {
            using (WatchdogTimer uut = new WatchdogTimer(500, "StopEvent"))
            {
                AutoResetEvent resetEvent = new AutoResetEvent(false);

                uut.OnTimeoutExpired += delegate()
                {
                    resetEvent.Set();

                    // By forcing Stop() to be called on itself, we can guarentee that
                    // we won't hit a racecondition anywhere, as we are stopping it on the timer thread.
                    uut.Stop();
                };

                uut.Start();

                Assert.IsTrue(resetEvent.WaitOne(2000));
                Assert.IsFalse(resetEvent.WaitOne(3000));

                uut.Start();

                Assert.IsTrue(resetEvent.WaitOne(2000));
                Assert.IsFalse(resetEvent.WaitOne(3000));
            }
        }
예제 #5
0
        /// <summary>
        ///     Disconnects the client.
        /// </summary>
        /// <param name="message">The optional message or reason for the disconnect.</param>
        /// <param name="exception">The optional Exception associated with the disconnect.</param>
        public void Disconnect(string message = null, Exception exception = null)
        {
            if (State != ConnectionState.Disconnected && State != ConnectionState.Disconnecting)
            {
                message = message ?? exception?.Message;

                ChangeState(ConnectionState.Disconnecting, message);

                InactivityTimer?.Stop();
                WatchdogTimer?.Stop();
                Stream?.Close();
                TcpClient?.Close();

                ChangeState(ConnectionState.Disconnected, message, exception);
            }
        }
예제 #6
0
        private void ClosedEventHandler(object sender, EventArgs e)
        {
            InputManager.Current.PreProcessInput -= GlobalClickEventHandler;
            if (_sourceWindow != null)
            {
                _sourceWindow.RemoveHook(WndProc);
            }

            if (_emergencyTimer != null)
            {
                _emergencyTimer.Stop();
                _emergencyTimer.Dispose();
            }

            if (_timeUpdateTimer != null)
            {
                _timeUpdateTimer.Stop();
            }
        }