C#使用定时器控制程序

Posted

技术标签:

【中文标题】C#使用定时器控制程序【英文标题】:C# Use timer to control program 【发布时间】:2018-09-05 11:54:44 【问题描述】:

我正在尝试创建一个类似于speedsum 站点的控制台应用程序。 speedsum是一个非常有用和有趣的网站,可以测试我们30多岁的数学能力。

经过几次尝试后,我只是想创建一个具有相同概念的小型 C# 控制台应用程序。

以下是我运行良好的代码。但我无法显示倒计时?!

我的代码:

static void Main(string[] args)
    
        int testCount = 0;

        Console.Write("\n Get.. Set... Go.... : This is a 30s test.. " +
            "Once each problem is completed the time finished will be shown \n Good Luck.. :) \n \n");
        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 1; i < 100000; i++)
        
            if (watch.Elapsed.TotalSeconds >= 30)
                break;
            TimeSpan timeSpan = TimeSpan.FromSeconds(Convert.ToInt32(watch.Elapsed.TotalSeconds));
            Console.Write($"\n timeSpan.ToString("c")" );

            Random r = new Random();
            int number1 = r.Next(10);
            int number2 = r.Next(10);
            int operation = r.Next(4);
            var method = (operation > 2) ? '+' : '*';
            int result = 0;
            result = method == '+' ? (number1 + number2) : (number1 * number2);             
            Console.Write($" \n number1 method number2 =  ");
            var getAnswer = Convert.ToInt32(Console.ReadLine());
            if (result == getAnswer)
            
                testCount++;
                continue;
            
            else
                break;
        
        watch.Stop();

        if(testCount >= 1 && testCount <=5)
            Console.Write($"\n No Worries!! Try Hard ... \n you have solved testCount problems \n");
        else if(testCount >=6 && testCount <=10)
            Console.Write($"\n Good!! You can do well next time ... \n you have solved testCount problems \n");
        else
            Console.Write($"\n Awesome!! You are really a Genius ... \n you have solved testCount problems \n");

        Console.Write("\n Thank you for playing with me... \n Enter a key to exit");
        Console.Read();
    

我想从 30 秒倒计时到 0 秒,

Get.. Set... Go.... : This is a 30s test.. Once each problem is completed the time finished will be shown 
Good Luck.. :)

<<Timer Should go here>> (30, 29... 0)

5 * 5 = 25 ... 

这个SO Question 展示了如何将倒计时加入我们的程序,但我对如何同时进行并行倒计时和给出问题感到困惑。

任何建议都会对我有所帮助。

【问题讨论】:

看看多线程,可能对你有帮助;) 【参考方案1】:

您似乎想使用BackgroundWorker。然后,DoWork 事件将减少剩余的秒数,而ProgressChanged 事件将报告当前进度。此解决方案的优点是后台工作人员正在异步运行,因此您不会阻塞主线程,允许用户随时输入答案。

private static int secondsLeft;
private static BackgroundWorker bgWorker;

static void Main(string[] args)

    secondsLeft = 30;
    bgWorker = new BackgroundWorker();
    bgWorker.DoWork += bgWorker_DoWork;
    bgWorker.ProgressChanged += bgWorker_ProgressChanged;
    bgWorker.WorkerReportsProgress = true;
    bgWorker.RunWorkerAsync();

    Console.ReadLine();


private static void bgWorker_DoWork(object sender, DoWorkEventArgs e)

    while (secondsLeft >= 0)
    
        bgWorker.ReportProgress(secondsLeft);
        Thread.Sleep(1000);
        secondsLeft--;
    


private static void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

    Console.WriteLine($"Seconds left: e.ProgressPercentage");

【讨论】:

谢谢 Dmitry,但这不是我需要的,我希望在不影响程序其他部分的情况下倒计时在一个地方运行 @JG'sSpark 一个地方是什么意思?控制台输出? ***.com/questions/18004902/…请看这个!! 但我需要重写倒计时值,同时与其他功能一起。

以上是关于C#使用定时器控制程序的主要内容,如果未能解决你的问题,请参考以下文章

C#定时器怎么写?

c#计时器用法

C# winform 两个定时器访问同一个变量

c# 定时器调用控件的事件处理事情出现卡死主界面如何解决

C# 定时器过早触发

C# 为啥定时器会自动停止