은 내가 지금까지 당신이 내 코드를 보여줌으로써 시작하자 : 나는 시도하고이 코드를 컴파일 할 때스레딩 : 스레드의 변수 액세스 및 종단
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 질문에 리드 :
정확히 내가 preanswer을하고 "프롬프트"방법으로 접근 답변을 어떻게해야합니까?
점수 변수에 1을 올바르게 추가 했습니까?
스레드가 실행중인 경우 어떻게 종료합니까? (이 경우 "ask"스레드는 응답을 입력 할 때까지 끝나지 않을 것입니다.)
변경 사항을 알려주십시오. 방금 몇 주 전에 시작했기 때문에 단어와 전문 용어를 잘 모릅니다. 되도록 명확하게 노력하십시오.
잠깐 - 메소드 외부에서 변수를 설정하겠습니까? 나는 그것이 가능하다는 것을 알지 못했다 ... –
그렇다, 그렇지 않으면 그것들은 당신의 스레드 방법에 보이지 않는다. –
아, 작동했습니다! "ask"스레드 종료에 대해 아는 것이 있습니까? –