【发布时间】: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