Exemple #1
0
        static void Main(string[] args)
        {
            int cnt1 = 0;
            int cnt2 = 0;

            var job1 = new TimerJob(() => Func("Job1", ref cnt1), 0.5);
            var job2 = new TimerJob(() => Func("Job2", ref cnt2), 1.0);
            var job3 = new TimerJob(() => throw new Exception("Job3"), 2.0);

            var scheduler = new TimerJobScheduler(GetLogger());

            scheduler.ThrowJobException(false);

            scheduler.AddJob(job1);
            scheduler.AddJob(job2);
            scheduler.AddJob(job3);

            scheduler.Start();

            Console.ReadKey();
            Console.WriteLine($"cnt1: {cnt1};  cnt2: {cnt2};");
        }
        public void TestRunning()
        {
            const int TIMER_PERIOD_MSEC = 100;
            const int COUNT             = 20;

            int cnt1 = 0;
            int cnt2 = 0;
            var job1 = new TimerJob(() => ++ cnt1, TIMER_PERIOD_MSEC * 0.001);
            var job2 = new TimerJob(() => ++ cnt2, TIMER_PERIOD_MSEC * 2 * 0.001);

            _scheduler.AddJob(job1);
            _scheduler.AddJob(job2);
            _scheduler.Start();

            Assert.Equal(0, cnt1);
            Assert.Equal(0, cnt2);

            Thread.Sleep(TIMER_PERIOD_MSEC * COUNT);

            Assert.True((cnt1 == COUNT) || (cnt1 == COUNT - 1));
            Assert.True((cnt2 == COUNT / 2) || (cnt2 == COUNT / 2 - 1));
        }