【问题标题】:How would I change my program so the timer rests after the method is correct我将如何更改我的程序,以便在方法正确后计时器停止
【发布时间】:2019-07-02 12:37:11
【问题描述】:

一旦方法正确,我将如何重置 30 秒,到目前为止,在每种方法正确之后,我都会中止线程,然后在下一个方法中启动一个新线程并启动它,但控制台仍然在 30 秒后关闭,而不是重置计时器? 我假设这是秒表计时器问题?

namespace Calculator
{

    class Program
    {

        static ThreadStart ThreadStart = new ThreadStart(Counter);
        static Thread Thread = new Thread(ThreadStart)
        {
            Priority = ThreadPriority.Highest
        };
        static void Main(string[] args)
        {

            Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
            "if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
            //These are the instructions
            Thread.Start();

            q1();
            q2();
        }

        static Stopwatch timer = Stopwatch.StartNew();
        static void Counter()
        {
            if (timer.ElapsedMilliseconds < 30000)
            {
                Thread.Sleep(1000);
                Counter();
            }
            else
            {
                Console.WriteLine("Too late");

                Environment.Exit(0);
            }

        }

        static void q1() //Return type is a string as a string prompting the user will ask them to try again
        {
            Console.WriteLine("1+1"); //This is the question
            int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable

            if (answer == 2) //If the users input is equal to 2 
            {
                Console.WriteLine("Correct");//Tells the user that they are correct
                Thread.Abort();
            }
            else
            {
                Console.WriteLine("Try again");
                q1();
            }
        }
        static void q2() //Return type is a string as a string prompting the user will ask them to try again
        {
            Thread Threadq2 = new Thread(ThreadStart);
            Threadq2.Start();
            Console.WriteLine("2+2"); //This is the question
            int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable

            if (answer == 4) //If the users input is equal to 2 
            {
                Console.WriteLine("Correct");//Tells the user that they are correct
                Thread.Abort();
            }
            else
            {
                Console.WriteLine("Try again");
                q1();
            }
        }
    }
}

【问题讨论】:

    标签: c# multithreading timer


    【解决方案1】:

    首先你在这里并不需要一个线程

    其次,Timer 可能不是您想要的,而且您似乎会找到一个更简单的测量时间的解决方案。

    public static DateTime _starTime;
    
    ...
    
    Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
            "if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
    
    _starTime = DateTime.Now();
    
    ...
    

    那么当你想检查时(即在用户回答后)你可以做这样的事情

    var seconds = DateTime.Now.Subtract(_starTime).TotalSeconds;
    
    If(seconds > 30)
    {
       // game over
    }
    

    【讨论】:

      【解决方案2】:

      为什么不将timer.Restart(); 放在 q1/q2 方法的开头?这样,每次您调用它们时计时器都会重新启动

      【讨论】:

        【解决方案3】:

        尝试使用Timer 并将间隔设置为 30 秒。在 Elapsed 事件中,只需关闭控制台即可。

        private static System.Timers.Timer timer = new System.Timers.Timer(2000);
                static void Main(string[] args)
                {
                    timer.Elapsed += Timer_Elapsed;
                    timer.Start();
        
                    // show instructions
                    while (true)
                    {
                       string answer = Console.ReadLine();
                        if (answer == "2") // check the answer somehow
                        {
                            timer.Stop(); // and restart the timer
                            timer.Start();
                            // show next question
                        }
                    }
        
                }
        
                private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
                {
                    Console.WriteLine("Too late");
                    timer.Elapsed -= Timer_Elapsed;
                    System.Threading.Thread.Sleep(2000);
                    Environment.Exit(0);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-17
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多