C#中的计时器无法正常工作

关西机器人

使用计时器(System.Timers.Timer)时,我遇到一个特殊的问题。我已经检查了代码,在推理中找不到错误所在。我非常感谢对此有一些见解。

我想做什么该程序在另一个内部有两个无限循环(一个叫做keepRunning,另一个叫做keepTrying)。没问题,如果我按Ctrl-C,则循环会变得有限,程序会正常结束。

在循环中,我不断要求用户按Enter。为什么?

好吧,我也有一个计时器。该计时器每3秒调用一次函数。如果这个函数被调用10倍keepTrying循环应该成为有限的,并且主程序退出这个循环。(通过在控制台中打印“不要尝试”和“开始尝试”可以清楚地表明这一点。

除非用户按回车键。如果他按Enter键,计时器将重置,并且计时器被调用的次数也将变为0。一切将重新开始。

好吧,它不起作用。以某种方式,即使计时器经过的事件被调用超过10次,它仍会被调用并且不会退出循环。

请看一下我的代码。我将不胜感激任何建议使其工作

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace TimerExample1
{
    class Program
    {
        private static Timer aTimer;
        private static bool keepRunning = true;
        private static bool keepTrying = true;
        private static int attempts = 0;

        static void Main(string[] args)
        {
            //We manage to set the way to finish this program with CTRL+C
            Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
            {
                e.Cancel = true; //execution continues after the delegate
                keepRunning = false;
                keepTrying = false;
            };

            SetTimer();

            while (keepRunning)
            {
                Console.WriteLine("Start trying");
                keepTrying = true;

                while (keepTrying)
                {
                    if (attempts > 10) break;

                    Console.WriteLine("Press the Enter key to prevent the timer from firing off...");
                    Console.ReadLine();
                    //it was pressed so
                    aTimer.Stop();
                    aTimer.Start();
                    attempts = 0;

                }//keep trying


                Console.WriteLine("got out of trying");
            }//keepRunning

            Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
            Console.WriteLine("\nPress the Enter key to exit the application...");

            aTimer.Stop();
            aTimer.Dispose();
            Console.ReadLine();

            Console.WriteLine("Terminating the application...");

        }
        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(3000);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                              e.SignalTime);

            attempts++;
            if (attempts > 10) keepTrying = false;
        }
    }
}
谢尔盖·L

您的代码中的问题Console.ReadLine();是阻止了操作。它会在该位置停止,直到用户按下Enter键。keepTrying = false;循环中不检查计时器递增尝试次数和设置其值的次数并不重要,因为您被阻止了ReadLine您应该将其更改Console.KeyAvailable为不阻塞使用。如果有密钥,Console.ReadKey则将其与Enter进行比较。请注意,如果有键,则应阅读它,否则,直到清空输入缓冲区之前,您将一直获得KeyAvailable。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章