gpt4 book ai didi

c# - .Net 中的控制台问题

转载 作者:行者123 更新时间:2023-11-30 13:34:44 25 4
gpt4 key购买 nike

我刚刚学习如何使用 C# 中的线程机制。我从 msdn 上拿了一个例子,并对其进行了修改,设计了一个像这样的简单游戏。但问题是 DoWork 方法中的 while 循环甚至在从主程序调用 DoWork 方法之前就读取键。IE。当您在屏幕上出现“Go:”之前运行程序时,如果您在 DoWork 方法中循环读取键时键入一些内容。但是在屏幕上打印“Go:”后,控制应该传递给 while 循环,对。有人可以解释一下为什么会这样吗?谢谢。

 public class Worker    
{
ConsoleKeyInfo cki;
StringBuilder sb = new StringBuilder();
bool f = true;

// This method will be called when the thread is started.
public void DoWork()
{
Console.SetCursorPosition(49, 8);
Console.Write("Go:");
Console.SetCursorPosition(53, 8);

while (!_shouldStop)
{
cki = Console.ReadKey(true);
if (f == true && (65 <= Convert.ToInt32(cki.Key) && Convert.ToInt32(cki.Key) <= 90))
{
Console.Write(cki.Key.ToString());
sb.Append(cki.Key.ToString());
}
}
while (true)
{
if (cki.Key.ToString() == "Enter") break;
cki = Console.ReadKey(true);
if (cki.Key.ToString() == "Enter") break;
}
}
public void RequestStop(string word)
{
_shouldStop = true;
f =false;
Console.WriteLine();
Console.SetCursorPosition(44, 10);
Console.WriteLine("- TIME OUT -");
Console.SetCursorPosition(46, 12);
if (sb.ToString() == word.ToUpper())
{
Console.WriteLine("CORRECT !");
Console.SetCursorPosition(42, 13);
Console.WriteLine("CONGRATULATIONS");
}
else { Console.SetCursorPosition(47, 12); Console.WriteLine("WRONG !"); }
Console.SetCursorPosition(40, 15);
Console.WriteLine("Press [Enter] to quit");
Console.CursorVisible = false;
}

private volatile bool _shouldStop;

}

public class WordPlay
{
static void Main()
{
Console.SetBufferSize(100,50);
Console.SetWindowSize(100,50);
string[] words = { "angstrom", "abacinate", "abactinal", "abandoned", "Babesiidae", "babirussa", "Babylonian", "bacchantic", "cabassous", "cableway" };
string word="";
Random randObj = new Random(0);
Console.SetCursorPosition(40, 6);
Console.WriteLine("Your String:");
Console.WriteLine();
for (int j = 0; j < 20; j++)
{
System.Threading.Thread.Sleep(150); Console.SetCursorPosition(53, 6);
Console.Write(words[randObj.Next(words.Length - 1)].ToUpper() + " ");
}

word = words[randObj.Next(words.Length - 1)].ToUpper();
Console.SetCursorPosition(53, 6);
Console.Write( word+" ");
Console.WriteLine();

// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);

// Start the worker thread.
workerThread.Start();

// Loop until worker thread activates.
while (!workerThread.IsAlive);

// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:

Thread.Sleep(3000);
// Request that the worker thread stop itself:

workerObject.RequestStop(word);

// Use the Join method to block the current thread
// until the object's thread terminates.

workerThread.Join();
}

最佳答案

乔·阿尔巴哈里的 free ebook on threading是一个很好的介绍。

关于c# - .Net 中的控制台问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2362980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com