Exemplo n.º 1
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");
        }
Exemplo n.º 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");
        }
Exemplo n.º 3
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");
        }
Exemplo n.º 4
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");
        }
Exemplo n.º 5
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");
        }
Exemplo n.º 6
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");
        }
Exemplo n.º 7
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");
        }
Exemplo n.º 8
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");
        }
Exemplo n.º 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");
        }
 public void SyncAction_QueuedBeforeStart_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.DoSynchronously(() => { });
     }
 }
 public void SyncAction_QueuedAfterJoin_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Join();
         thread.DoSynchronously(() => { });
     }
 }
        public void SyncAction_QueuedAfterStart_IsExecutedByThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() => { threadId = Thread.CurrentThread.ManagedThreadId; });

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
Exemplo n.º 13
0
    static void Main()
    {
        ActionThread actionThread = new ActionThread();

        // Set IsBackground so that this thread won't prevent the program from exiting
        actionThread.IsBackground = true;
        actionThread.Start();

        // Wait 100 ms for the ActionThread to sleep for 1 minute
        bool done = actionThread.DoSynchronously(() => Thread.Sleep(TimeSpan.FromMinutes(1)), TimeSpan.FromMilliseconds(100));

        Console.WriteLine("ActionThread completed action synchronously: " + done);
    }
Exemplo n.º 14
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");
        }
Exemplo n.º 15
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");
        }
Exemplo n.º 16
0
    static void Main()
    {
        using (ActionThread actionThread = new ActionThread())
        {
            actionThread.Start();

            Console.WriteLine("Console thread ID: " + Thread.CurrentThread.ManagedThreadId);

            // Any delegates passed to ActionThread.DoSynchronously are run by that ActionThread (synchronously)
            int actionThreadID = 0;
            actionThread.DoSynchronously(() => actionThreadID = Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("ActionThread thread ID: " + actionThreadID);
        }
    }
Exemplo n.º 17
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");
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
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");
        }
Exemplo n.º 20
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");
        }
Exemplo n.º 21
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");
        }
Exemplo n.º 22
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");
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
Exemplo n.º 24
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");
        }
Exemplo n.º 25
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");
        }
Exemplo n.º 26
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");
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                    {
                        using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                        {
                            GenericSynchronizingObject test = new GenericSynchronizingObject();
                            IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                            test.EndInvoke(result);
                        }
                    });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
Exemplo n.º 30
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");
        }
Exemplo n.º 31
0
        public void SyncAction_QueuedAfterStart_IsExecutedByThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() => { threadId = Thread.CurrentThread.ManagedThreadId; });

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
Exemplo n.º 32
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");
        }
Exemplo n.º 33
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");
        }
Exemplo n.º 34
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");
        }
Exemplo n.º 35
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");
        }
Exemplo n.º 36
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");
        }
Exemplo n.º 37
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");
        }
Exemplo n.º 38
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");
        }
Exemplo n.º 39
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");
        }
Exemplo n.º 40
0
 public void SyncAction_QueuedAfterJoin_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Join();
         thread.DoSynchronously(() => { });
     }
 }
Exemplo n.º 41
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");
        }
Exemplo n.º 42
0
 public void SyncAction_QueuedBeforeStart_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.DoSynchronously(() => { });
     }
 }
Exemplo n.º 43
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");
        }
Exemplo n.º 44
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");
        }
Exemplo n.º 45
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");
        }