在同一位置编写控制台应用程序的进度 [英] Writing Progress in console application at the same location
问题描述
我确实有一个控制台应用程序应该通知用户正在进行的下载进度。输出如下:
总下载进度:12%[32 MB到目前为止]
我正在更新"12%[目前为止32 MB]"在"总下载进度:"之后的部分部分数据从网络到达时我正在使用"Console.Write(text)"在"总下载进度:"之后写
part并返回冒号后面的点:zh
System.Console.SetCursorPosition(  ;
System .Console.CursorLeft - text.Length,
System.Console.CursorTop);
这里的事情是我得到一个"值必须大于或等于零且小于控制台的缓冲区大小在该维度。
参数名称:left
实际值为-26。"
$
异常为一旦我更改控制台大小或文本从控制台屏幕缓冲区增长或包装到下一行作为soons我更改控制台大小。如何防止此异常以及如何实现文本更新例程和/或
游标位置处理?任何想法都会很好。
提前付款
K.
& quot;此回复按原样提供,不附带任何明示或暗示& quot;
这是你想要的事情吗?
使用System;
使用System.Threading;
namespace $ $
class program
{
static void Main(string [] args)
{
//写出字符串的常量部分。
//使用Write()而不是WriteLine(),因此光标停留在行的末尾。
Console.Write(" Total download progress:");
//获取光标的位置。它应该在字符串的末尾。
int left = Console.CursorLeft;
int top = Console.CursorTop;
//模拟进度的循环。
for(int i = 1; i< 11; i ++)
{
//每次将光标位置设置为相同的位置。
Console.SetCursorPosition(左,上);
//只是一些虚拟值来说明这一点。
int percent = i * 10;
int mbytes = i * 12;
string progress = string.Format(" {0}%[{1} MBytes到目前为止]",percent,mbytes);
Console.Write(进度);
//在下次写入前暂停半秒钟。
Thread.Sleep(500);
}
Console.WriteLine(" \ n");
}
}
}
Hi,
i do have a console application that should notify the user of a ongoing download progress. The output looks like this:
Total download progress: 12% [32 MBytes so far]
I am updating the "12% [32 MBytes so far]" part right after the "Total download progress:" part as soon as some data arrives from the network. I am using "Console.Write(text)" to write after the "Total download progress:"
part and return to the point right after the colon with:
System.Console.SetCursorPosition(
System.Console.CursorLeft - text.Length,
System.Console.CursorTop);
The thing here is that i get a "The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: left
Actual value was -26."
Exception as soon as i change the console size or the text grows out of the consoles screen buffer or wraps to the next line as soons as i change the console size. How can i prevent this exception and how should i implement the text update routine and/or the
cursor position handling? Any ideas would be nice.
Thanks in advance
K.
"This reply is provided as is, without warranty express or implied"
Is this the sort of thing you're after?
using System; using System.Threading; namespace Project { class Program { static void Main(string[] args) { // Write the constantant part of the string. // Use Write(), not WriteLine(), so the cursor stays at the end of the line. Console.Write("Total download progress : "); // Get the location of the cursor. It should be at the end of the string. int left = Console.CursorLeft; int top = Console.CursorTop; // A loop to simulate progress. for (int i = 1; i < 11; i++) { // Set the cursor position to the same position each time. Console.SetCursorPosition(left, top); // Just some dummy values to illustrate the point. int percent = i * 10; int mbytes = i * 12; string progress = string.Format("{0}% [{1}MBytes so far]", percent, mbytes); Console.Write(progress); // Pause for half a second before the next write. Thread.Sleep(500); } Console.WriteLine("\n"); } } }
这篇关于在同一位置编写控制台应用程序的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!