简体   繁体   English

x秒后停止循环线程方法

[英]Stop a looped thread method after x seconds

I'm creating a console game as simple as "I generate a random number, find it", but with many options. 我正在创建一个控制台游戏,就像“我生成一个随机数,找到它”一样简单,但是有很多选择。

My current code (without what I want here) is availlable on GitHub: https://github.com/crakmaniaque/trouvezmoi 我当前的代码(这里没有我想要的)可以在GitHub上找到: https//github.com/crakmaniaque/trouvezmoi

What I want is to create a version of my game which will be timed, so the computer generates numbers, the user finds it, it generates a new one and the player have 90 seconds to find a max lot of random numbers. 我想要的是创建一个可以定时的游戏版本,以便计算机生成数字,用户找到它,生成一个新的数字,并且玩家有90秒的时间来找到最大数量的随机数。 I can code this easily. 我可以轻松地编写代码。

What I will need help is to stop the game (a thread) after 90 seconds and retrieve the number of answers founded from the thread. 我将需要帮助的是90秒后停止游戏(一个线程),并从该线程中检索建立的答案数量。 The Console.Title should also show time remaining. Console.Title还应该显示剩余时间。 The attempt I've tried works, but the thread is not interrupted if console is asking for number input ( Console.ReadLine() ). 我尝试过的尝试有效,但是如果控制台要求输入数字( Console.ReadLine() ),则线程不会中断。 But the timer is for the entire process, not only user input. 但是计时器用于整个过程,而不仅仅是用户输入。

private static void timerb()
{
     int t = 90;
     for (int i = 0; i < 90; i++)
     {
         Console.Title = t + " seconds remaining";
         Thread.Sleep(1000);
         t--;
     }
}
private static void cGame()
{
    Thread t = new Thread(timerb);
    t.Start();
    while (t.IsAlive)
    {
        bool good = false;
        int rnd = new Random().Next(0,10); // 0 and 10 are sample 
        while (!good)
        {
            try
            {
                Console.Write("Enter a number between x and y >");
                int i = int.Parse(Console.ReadLine());
                if (i == rnd)
                {
                    good = true;
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid answer.");
            }
        }
    }
}

I don't know much about threading and at that point I'm stuck. 我对线程了解不多,因此我陷入了困境。 Can someone help me with my problem? 有人可以帮我解决我的问题吗? I'm using .NET 2.0. 我正在使用.NET 2.0。

Perhaps you are looking for a timer? 也许您正在寻找计时器? You could register an event, that would fire after 90 seconds, that would run while the loop is happening. 您可以注册一个事件,该事件将在90秒后触发,并在循环发生时运行。 The documentation can be found here: Timer class MSDN documentation . 该文档可以在这里找到: 计时器类MSDN文档

I believe the usage would be: 我相信用法是:

Timer timer = new Timer { Interval = new Timespan (0,1,30);
timer.elapsed += //function to fire to kill the app or the game

You'd need to make each console read with a timeout equal to the amount of time left in the game. 您需要使每个控制台读取的超时时间等于游戏中剩余的时间。 That solves that issue. 那解决了那个问题。

Then, you need a way to signal the timerb thread to shut down when the main game loop has ended. 然后,您需要一种在主游戏循环结束时向Timerb线程发出信号的方法。 I think the simplest way would be to end the game loop when the remaining time is <= zero. 我认为最简单的方法是在剩余时间<=零时结束游戏循环。 Alternatively, you could make timerb singnal the main thread to shut down when t == 0 . 或者,您可以使timerb信号作为t == 0时关闭的主线程。 Inter-thread communication is always complicated and error-prone, though. 但是,线程间通信总是很复杂且容易出错。

You can signal the timerb thread to shut down by setting a volatile bool shutdown to true and by making timerb poll that variable and shut itself down. 您可以通过将volatile bool shutdown设置为true并通过使timerb轮询该变量并自行关闭来向timerb线程发出关闭信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM