猿问

如何更新C#Windows控制台应用程序中的当前行?

如何更新C#Windows控制台应用程序中的当前行?

在C#中构建Windows控制台应用程序时,是否可以写入控制台而无需扩展当前行或转到新行?例如,如果我想显示一个百分比表示一个进程完成的接近程度,我只想更新与光标在同一行的值,而不必将每个百分比放在一个新行上。

这可以通过“标准”C#控制台应用程序完成吗?


汪汪一只猫
浏览 918回答 3
3回答

UYOU

到目前为止,我们有三种竞争方案可供选择:Console.Write("\r{0}&nbsp; &nbsp;", value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Option 1: carriage returnConsole.Write("\b\b\b\b\b{0}", value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Option 2: backspace{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Option 3 in two parts:&nbsp; &nbsp; Console.SetCursorPosition(0, Console.CursorTop);&nbsp; &nbsp;// - Move cursor&nbsp; &nbsp; Console.Write(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // - Rewrite}我总是使用Console.CursorLeft = 0,第三种选择的变种,所以我决定做一些测试。这是我使用的代码:public&nbsp;static&nbsp;void&nbsp;CursorTest(){ &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;testsize&nbsp;=&nbsp;1000000; &nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("Testing&nbsp;cursor&nbsp;position"); &nbsp;&nbsp;&nbsp;&nbsp;Stopwatch&nbsp;sw&nbsp;=&nbsp;new&nbsp;Stopwatch(); &nbsp;&nbsp;&nbsp;&nbsp;sw.Start(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;testsize;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Write("\rCounting:&nbsp;{0}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;sw.Stop(); &nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("\nTime&nbsp;using&nbsp;\\r:&nbsp;{0}",&nbsp;sw.ElapsedMilliseconds); &nbsp;&nbsp;&nbsp;&nbsp;sw.Reset(); &nbsp;&nbsp;&nbsp;&nbsp;sw.Start(); &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;top&nbsp;=&nbsp;Console.CursorTop; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;testsize;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.SetCursorPosition(0,&nbsp;top);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Write("Counting:&nbsp;{0}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;sw.Stop(); &nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("\nTime&nbsp;using&nbsp;CursorLeft:&nbsp;{0}",&nbsp;sw.ElapsedMilliseconds); &nbsp;&nbsp;&nbsp;&nbsp;sw.Reset(); &nbsp;&nbsp;&nbsp;&nbsp;sw.Start(); &nbsp;&nbsp;&nbsp;&nbsp;Console.Write("Counting:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;testsize;&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Write("\b\b\b\b\b\b\b\b{0,8}",&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;sw.Stop(); &nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine("\nTime&nbsp;using&nbsp;\\b:&nbsp;{0}",&nbsp;sw.ElapsedMilliseconds);}在我的机器上,我得到以下结果:退格:25.0秒回车:28.7秒SetCursorPosition:49.7秒此外,SetCursorPosition引起了明显的闪烁,我没有观察到任何一种替代方案。因此,道德是尽可能使用退格或回车,并感谢教我一个更快的方法来做到这一点,所以!更新:在评论中,Joel建议SetCursorPosition相对于移动的距离是恒定的,而其他方法是线性的。进一步的测试证实了这种情况,然而恒定的时间和缓慢仍然很慢。在我的测试中,将一长串退格写入控制台比SetCursorPosition更快,直到大约60个字符。因此,退格会更快地替换短于60个字符(或左右)的部分行,并且它不会闪烁,所以我将支持我最初认可的\ b over \ r \ n和SetCursorPosition。
随时随地看视频慕课网APP
我要回答