C# 多线程问题
我有两种方法C# 多线程问题,c#,multithreading,C#,Multithreading,我有两种方法 public void L1Timer() { Console.Clear(); int score = tot; Console.Write("Chances : " + ch); Console.CursorLeft = 40; Console.Write("Marks : " + score); for (int time = 0; time <= 10000
public void L1Timer()
{
Console.Clear();
int score = tot;
Console.Write("Chances : " + ch);
Console.CursorLeft = 40;
Console.Write("Marks : " + score);
for (int time = 0; time <= 100000; time++)
{
Console.SetCursorPosition(65, 0);
Console.Write("Time Elapsed : " + time + " Secs");
Console.CursorLeft = 40;
stime = time;
Thread.Sleep(1000);
Console.Beep();
//Level1();
}
}
public void Level1()
{
Console.WriteLine("\n\n");
Console.CursorLeft = 40;
Console.WriteLine("C _ _ E _ _ _ T _ _ N");
Console.WriteLine("\n\n");
tot = 0;
while ((tot <= 70) && (ch > 0))
{
Console.Write("Guess : ");
string gues = Console.ReadLine();
switch (gues)
{
case "E": tot += 10; ch--; L1Timer(); Level1(); break;
case "L": tot += 10; ch--; break;
case "B": tot += 10; ch--; break;
case "R": tot += 10; ch--; break;
case "A": tot += 10; ch--; break;
case "I": tot += 10; ch--; break;
case "O": tot += 10; ch--; break;
default: tot += 0; ch--; break;
}
Console.WriteLine();
}
}
但是它没有按照我想要的方式工作。它执行了第一个和第二个,但将光标移回第一个方法,这将不允许用户键入第二个方法并回答问题…请帮助我
运行它并观察右上角的时间流逝。点击一些字母并检查分数/机会。点击Escape,然后点击“q”或“r”,看看会发生什么
这只是一个简单的示例,用于演示“奇特”控制台界面的流程我在这个“游戏”背后几乎没有任何真正的逻辑,而你在最初的问题描述中还没有以某种方式发布过这个“游戏”;因此,我不认为这是“做你的家庭作业。”<强>这只是游戏的“绒毛”。真正的作业可能会让你将游戏逻辑放入类中。如果将该类实例声明为静态,就像我对“Score”和“Chances”变量所做的那样,那么除了Main()之外的不同例程可以访问它,并使用类“game state”中的值更新屏幕。开发代表棋盘和当前游戏状态的类通常是作业的核心,也是您真正应该关注的地方
希望对你有帮助
class Program
{
static int Score = 0;
static int Chances = 10;
static int promptX, promptY;
static string board = "{No Board Loaded}";
static System.Diagnostics.Stopwatch SW = new Stopwatch();
static void Main(string[] args)
{
ConsoleKeyInfo cki;
Console.Title = "Some Word Game";
bool quit = false;
bool gameWon = false;
bool gameOver = false;
while (!quit)
{
Reset();
ShowBoard("C _ _ E _ _ _ T _ _ N");
gameWon = false;
gameOver = false;
while (!gameOver)
{
UpdateStats();
// make it appear as though the cursor is waiting after the prompt:
Console.SetCursorPosition(promptX, promptY);
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true); // don't display key
if (cki.Key == ConsoleKey.Escape)
{
gameOver = true;
}
else
{
// if it's A thru Z...
if (char.IsLetter(cki.KeyChar))
{
string key = cki.KeyChar.ToString().ToUpper();
Console.Write(key);
switch (key)
{
case "E":
Score += 10;
Chances--;
break;
case "L":
Score += 10;
Chances--;
break;
case "B":
Score += 10;
Chances--;
break;
case "R":
Score += 10;
Chances--;
break;
case "A":
Score += 10;
Chances--;
break;
case "I":
Score += 10;
Chances--;
break;
case "O":
Score += 10;
Chances--;
break;
default:
Score += 0;
Chances--;
break;
}
// ... possibly update the board somehow in here? ...
// ... some board logic ...
// ShowBoard("updated board in here");
// set gameOver to drop out of loop
// also set gameWon if the user beat the board
}
else
{
Console.Write(" ");
}
}
}
System.Threading.Thread.Sleep(200);
}
if (gameWon)
{
// ... do something ...
}
else
{
// ... do something else ...
}
quit = QuitProgram();
}
}
static void Reset()
{
// reset game variables and clock:
Score = 0;
Chances = 10;
SW.Restart();
Console.Clear();
CenterPrompt("Guess: ");
promptX = Console.CursorLeft;
promptY = Console.CursorTop;
}
static void ShowBoard(string NewBoard)
{
board = NewBoard;
Console.SetCursorPosition(Console.WindowWidth / 2 - board.Length / 2, promptY - 2);
Console.Write(board);
}
static void UpdateStats()
{
// hide cursor while we update the stats:
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.Write("Score: " + Score.ToString("000"));
Console.SetCursorPosition(35, 0);
Console.Write("Chances: " + Chances.ToString("00"));
TimeSpan ts = SW.Elapsed;
string totalTime = String.Format("Time Elapsed: {0}:{1}:{2}", ts.Hours, ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
Console.SetCursorPosition(Console.WindowWidth - totalTime.Length, 0);
Console.Write(totalTime);
// ... add other output statistics in here? ...
// turn cursor back on for the prompt:
Console.CursorVisible = true;
}
static void CenterPrompt(string message)
{
Console.SetCursorPosition(Console.WindowWidth / 2 - message.Length / 2, Console.WindowHeight / 2);
Console.Write(message);
}
static bool QuitProgram()
{
Console.Clear();
CenterPrompt("Thanks for playing! Press 'q' to Quit, or 'r' to Retry.");
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
switch (cki.KeyChar.ToString().ToUpper())
{
case "Q":
return true;
case "R":
return false;
}
}
}
}
}
你从不同的线程访问控制台会导致死锁。请问我能做些什么?你应该看看这个类。我在处理WPF应用程序时遇到了一些麻烦,可能会对您有所帮助。不要使用
Console.ReadLine()
。相反,设置一个主循环(都在同一个线程中…不需要不同的循环)来更新时间/分数/etc,并使用Console.KeyAvailable
和Console.ReadKey()
检查输入。您不应该为L1Timer
方法创建线程。相反,您应该创建一个每秒触发一次的计时器。谢谢,但当我运行应用程序时,计时器不计时,不知道你做错了什么。您是否从一个新的空白控制台项目开始,并将此代码粘贴到其上?
class Program
{
static int Score = 0;
static int Chances = 10;
static int promptX, promptY;
static string board = "{No Board Loaded}";
static System.Diagnostics.Stopwatch SW = new Stopwatch();
static void Main(string[] args)
{
ConsoleKeyInfo cki;
Console.Title = "Some Word Game";
bool quit = false;
bool gameWon = false;
bool gameOver = false;
while (!quit)
{
Reset();
ShowBoard("C _ _ E _ _ _ T _ _ N");
gameWon = false;
gameOver = false;
while (!gameOver)
{
UpdateStats();
// make it appear as though the cursor is waiting after the prompt:
Console.SetCursorPosition(promptX, promptY);
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true); // don't display key
if (cki.Key == ConsoleKey.Escape)
{
gameOver = true;
}
else
{
// if it's A thru Z...
if (char.IsLetter(cki.KeyChar))
{
string key = cki.KeyChar.ToString().ToUpper();
Console.Write(key);
switch (key)
{
case "E":
Score += 10;
Chances--;
break;
case "L":
Score += 10;
Chances--;
break;
case "B":
Score += 10;
Chances--;
break;
case "R":
Score += 10;
Chances--;
break;
case "A":
Score += 10;
Chances--;
break;
case "I":
Score += 10;
Chances--;
break;
case "O":
Score += 10;
Chances--;
break;
default:
Score += 0;
Chances--;
break;
}
// ... possibly update the board somehow in here? ...
// ... some board logic ...
// ShowBoard("updated board in here");
// set gameOver to drop out of loop
// also set gameWon if the user beat the board
}
else
{
Console.Write(" ");
}
}
}
System.Threading.Thread.Sleep(200);
}
if (gameWon)
{
// ... do something ...
}
else
{
// ... do something else ...
}
quit = QuitProgram();
}
}
static void Reset()
{
// reset game variables and clock:
Score = 0;
Chances = 10;
SW.Restart();
Console.Clear();
CenterPrompt("Guess: ");
promptX = Console.CursorLeft;
promptY = Console.CursorTop;
}
static void ShowBoard(string NewBoard)
{
board = NewBoard;
Console.SetCursorPosition(Console.WindowWidth / 2 - board.Length / 2, promptY - 2);
Console.Write(board);
}
static void UpdateStats()
{
// hide cursor while we update the stats:
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.Write("Score: " + Score.ToString("000"));
Console.SetCursorPosition(35, 0);
Console.Write("Chances: " + Chances.ToString("00"));
TimeSpan ts = SW.Elapsed;
string totalTime = String.Format("Time Elapsed: {0}:{1}:{2}", ts.Hours, ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
Console.SetCursorPosition(Console.WindowWidth - totalTime.Length, 0);
Console.Write(totalTime);
// ... add other output statistics in here? ...
// turn cursor back on for the prompt:
Console.CursorVisible = true;
}
static void CenterPrompt(string message)
{
Console.SetCursorPosition(Console.WindowWidth / 2 - message.Length / 2, Console.WindowHeight / 2);
Console.Write(message);
}
static bool QuitProgram()
{
Console.Clear();
CenterPrompt("Thanks for playing! Press 'q' to Quit, or 'r' to Retry.");
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
switch (cki.KeyChar.ToString().ToUpper())
{
case "Q":
return true;
case "R":
return false;
}
}
}
}
}