Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Console.Clear()和多线程在C#中倒计时_C#_C# 4.0_Console Application - Fatal编程技术网

如何使用Console.Clear()和多线程在C#中倒计时

如何使用Console.Clear()和多线程在C#中倒计时,c#,c#-4.0,console-application,C#,C# 4.0,Console Application,我是C#的初学者。我正在开发一个控制台游戏,但C#中的线程有问题 我的游戏将显示一个顶部的酒吧倒计时运行。我尝试使用一个线程,使用Console.Clear()清除旧编号,然后在一行(59,58,57…)上用新编号替换。我的游戏会在中央屏幕或任何地方显示一条信息,以便用户输入用户数据,等等。但是,当我开始线程倒计时时,它会清除控制台屏幕,也会清除用户可以输入用户数据的消息。你能帮我解释一下如何启动2个线程,做更多不同的任务吗 using System; using System.Threadin

我是C#的初学者。我正在开发一个控制台游戏,但C#中的线程有问题

我的游戏将显示一个顶部的酒吧倒计时运行。我尝试使用一个线程,使用Console.Clear()清除旧编号,然后在一行(59,58,57…)上用新编号替换。我的游戏会在中央屏幕或任何地方显示一条信息,以便用户输入用户数据,等等。但是,当我开始线程倒计时时,它会清除控制台屏幕,也会清除用户可以输入用户数据的消息。你能帮我解释一下如何启动2个线程,做更多不同的任务吗

using System; using System.Threading;
namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
        Program m = new Program();
        Thread pCountDown = new Thread(new ThreadStart(
            m.DisplayCountDown   
        ));
        Thread pDisplayForm = new Thread(new ThreadStart(
            m.DisplayForm    
        ));
        pCountDown.Start();
        pDisplayForm.Start();
        Console.ReadKey();
    }

    private void DisplayCountDown() {
        for (int i = 60; i >= 0; --i) {
            Console.Write("Time: {0}",i);
            Thread.Sleep(1000);
            Console.Clear();
        }

    }

    private void DisplayForm() {
        while (true) {
            Console.Write("Enter your number: ");
            int a = Int32.Parse(Console.ReadLine());
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
 }
}
错误:

我想要这样:


图片(对不起,我是新会员):

您不需要线程,也不需要清除控制台。只需按照建议使用
Console.SetCursorPosition()
Console.Write()
,就可以覆盖数字。

下面是一个示例
DisplayCountDown
,它不会每秒清除整个屏幕:

private void DisplayCountDown()
{
    for (int i = 20; i >= 0; --i)
    {
        int l = Console.CursorLeft;
        int t = Console.CursorTop;
        Console.CursorLeft = 0;
        Console.CursorTop = 0;
        Console.Write("Time: {0}    ", i);
        Console.CursorLeft = l;
        Console.CursorTop = t;
        Thread.Sleep(1000);
    }
}
然而,这仍然存在一些问题。在我的例子中,我看到“输入您的号码”出现在顶行并被覆盖,因此不得不添加该行

if (Console.CursorTop == 0) Console.CursorTop = 1;
while
循环中。此外,如果用户输入足够的数字,倒计时将从视图中滚出,如果您尝试向上滚动查看倒计时,则设置光标位置将自动向后滚动


我还在
int.Parse
抛出异常时遇到间歇性问题,这可能是由于用户输入的某个关键点的倒计时发生变化造成的。

您无需清除控制台
Console.Write()
覆盖现有字符,因此只需使用
Console.SetCursorPosition(x,y)
更改光标位置即可

例如:

string mystring = "put what you want to right here";
Console.SetCursorPosition(0,0); //the position starts at 0 just make a note of it
Conolse.Write(mystring);

//now when you are ready to clear the text and print something over it again
//just add this

//now first erase the previous text
for(int i = 0; i< mystring.Length; i++)
{
    Console.SetCursorPosition(i,0);
    Console.Write(' ');
}

//now write your new text
mystring = "something else";
Console.SetCursorPosition(0,0);
Console.Write("mystring");
string mystring=“把你想要的放在这里”;
控制台。设置光标位置(0,0)//这个位置从0开始,记下来就行了
编写(mystring);
//现在,当您准备清除文本并再次打印内容时
//再加上这个
//现在首先删除前面的文本
for(int i=0;i
我当然不是控制台显示方面的专家,所以希望有人能提出更好的建议。但看起来至少你需要在每个倒计时步骤上重新绘制整个屏幕,而不仅仅是计时器。即使这样,您也会遇到每秒清除用户输入的问题(或者至少清除用户输入的可见性,这可能会导致奇怪的用户体验)。我不确定是否有办法切断控制台的部分以进行清理。。。