コード例 #1
0
        public void Start()
        {
            ServiceA a       = new ServiceA();
            ServiceB b       = new ServiceB();
            ServiceC c       = new ServiceC();
            int      maxTime = 3000;

            var timerA = new Timer(maxTime - a.ResponseTime);

            timerA.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) =>
            {
                Console.WriteLine("### Timer Stopped ### \n");
                timerA.Stop();
                Console.WriteLine("### Scheduled Task Started ### \n\n");
                a.DoWork();
                Console.WriteLine($"{DateTime.Now.TimeOfDay}### Task A Finished ### \n\n");
            });

            var timerB = new Timer(maxTime - b.ResponseTime);

            timerB.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) =>
            {
                Console.WriteLine("### Timer Stopped ### \n");
                timerB.Stop();
                Console.WriteLine("### Scheduled Task Started ### \n\n");
                b.DoWork();
                Console.WriteLine($"{DateTime.Now.TimeOfDay}### Task B Finished ### \n\n");
            });

            var timerC = new Timer(maxTime - c.ResponseTime);

            timerC.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) =>
            {
                Console.WriteLine("### Timer Stopped ### \n");
                timerC.Stop();
                Console.WriteLine("### Scheduled Task Started ### \n\n");
                c.DoWork();
                Console.WriteLine($"{DateTime.Now.TimeOfDay}### Task C Finished ### \n\n");
            });

            timerA.Start();
            timerB.Start();
            timerC.Start();

            //timerA.Dispose();
            //timerB.Dispose();
            //timerC.Dispose();

            Console.ReadLine();
            Console.WriteLine("END");
        }
コード例 #2
0
        public static void Start()
        {
            ServiceA a = new ServiceA();
            ServiceB b = new ServiceB();
            ServiceC c = new ServiceC();

            int maxDelay = 3000;
            var sw       = new Stopwatch();

            sw.Start();

            Task taskA = Task.Delay(maxDelay - a.ResponseTime).ContinueWith(x =>
            {
                Console.WriteLine("a");
                a.DoWork();
                Console.WriteLine($"taskA ready at {sw.Elapsed}");
            });

            Task taskB = Task.Delay(maxDelay - b.ResponseTime).ContinueWith(x =>
            {
                Console.WriteLine("b");
                b.DoWork();
                Console.WriteLine($"taskB ready at {sw.Elapsed}");
            });

            Task taskC = Task.Delay(maxDelay - c.ResponseTime).ContinueWith(x =>
            {
                Console.WriteLine("c");
                c.DoWork();
                Console.WriteLine($"taskC ready at {sw.Elapsed}");
            });

            Task.WaitAll(new Task[] { taskA, taskB, taskC });

            //taskA.Wait();
            //taskB.Wait();
            //taskC.Wait();
        }