线程:线程的可变访问和终止
首先让我向您展示我的代码:
using System;
using System.Threading;
class MathQuiz
{
static void Main()
{
int score = 0;
string preanswer;
decimal answer = 0;
Console.WriteLine("Welcome to Project5, a MathQuiz project.");
Console.WriteLine("You will be asked 10 questions, and will have 30 seconds to read and answer each one.");
Console.WriteLine("Press any key to begin.");
Console.ReadKey(true);
Console.WriteLine("What is 2 + 2?");
Thread ask = new Thread (new ThreadStart (MathQuiz.prompt));
ask.Start();
Thread.Sleep(3000);
//This is where I want to end the thread if it isn't already done.
if (answer == 4)
{
score = score+1; //Here's where I don't know if my adding is correct.
}
Console.WriteLine("Press any key to move on to the next question!");
Console.ReadKey(true);
}
static void prompt()
{
preanswer = (Console.ReadLine());
if (!decimal.TryParse(preanswer, out answer))
{
Console.WriteLine("That wasn't even a number or decimal!");
}
else
{
answer = decimal.Parse(preanswer);
}
}
}
因此,当我尝试编译此代码时,我会在preanswer中收到CS0103错误并在“提示”方法中回答。
这导致3个问题:
我必须做些什么才能使“提示”方法能够进行预告和回答?
我是否正确地在分数变量上加1?
如果线程正在运行,我该如何终止? (在这种情况下,“ask”线程在他们输入答案之前不会结束。)
请告诉我要改变什么。我不知道编码的单词和术语,因为我刚开始几周前。请尽量清楚。
没有找到相关结果
已邀请:
2 个回复
殿虫
等等
眠皇