C#中的控制台进度栏

ProgressDemo

介绍 (Introduction)

I write a lot of console apps, often tools and utilities that generate source code and other files. Sometimes, this generation can take some time and it would be nice to be able to report progress to the user.

我写了很多控制台应用程序,通常是生成源代码和其他文件的工具和实用程序。 有时,这一代可能会花费一些时间,并且能够向用户报告进度会很好。

I've seen a lot of progress bars for Winforms and WPF but not much of anything for the console, and yet it strikes me as something pretty useful.

我已经看到了许多Winforms和WPF的进度条,但对于控制台却没有太多的东西,但是它给我留下了非常有用的印象。

使用这个混乱 (Using this Mess)

You can call ConsoleUtility.WriteProgressBar() to report a progress with a known amount of work to be done, or ConsoleUtility.WriteProgress() to report a progress with an unknown amount of work to be done. The first parameter in any case is the progress. For the open ended progress (WriteProgress()), the first parameter is simply an integer value that gets incremented each time. For the bar, it's a number between 0 and 100, inclusive. The second parameter should be false the first time the method is called, and true for subsequent times. This demo code should demonstrate:

您可以调用ConsoleUtility.WriteProgressBar()报告要完成的工作量的进度,也可以调用ConsoleUtility.WriteProgressBar()以报告要完成的ConsoleUtility.WriteProgress()的进度。 无论如何,第一个参数是进度。 对于开放式进度( WriteProgress() ),第一个参数只是一个整数值,每次都会递增。 对于酒吧,它是介于0和100之间(包括0和100)的数字。 第二个参数应该是false第一次调用该方法,并true用于后续次。 该演示代码应演示:

ConsoleUtility.WriteProgressBar(0);
for (var i = 0; i <= 100; ++i)
{
    ConsoleUtility.WriteProgressBar(i,true);
    Thread.Sleep(50);
}
Console.WriteLine();
ConsoleUtility.WriteProgress(0);
for (var i = 0; i <= 100; ++i)
{
    ConsoleUtility.WriteProgress(i, true);
    Thread.Sleep(50);
}

编码此混乱 (Coding this Mess)

The code is short and sweet. The only non-intuitive bits are the use of backspace to overwrite our previous progress and the format string we use to pad the percentage with leading spaces.

代码简短而甜美。 唯一不直观的地方是使用退格键来覆盖我们之前的进度,以及使用格式字符串来用前导空格填充百分比的格式字符串。

using System;

namespace CU
{
    static class ConsoleUtility
    {
        const char _block = '■';
        const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
        const string _twirl = "-\\|/";
        public static void WriteProgressBar(int percent, bool update = false)
        {
            if(update)
                Console.Write(_back);
            Console.Write("[");
            var p = (int)((percent / 10f)+.5f);
            for (var i = 0;i<10;++i)
            {
                if (i >= p)
                    Console.Write(' ');
                else
                    Console.Write(_block);
            }
            Console.Write("] {0,3:##0}%", percent);    
        }
        public static void WriteProgress(int progress, bool update = false)
        {
            if (update)
                Console.Write("\b");
            Console.Write(_twirl[progress % _twirl.Length]);
        }
    }
}

This is the code in its entirety, so you don't really even have to download the link. Just copy this if you like.

这是完整的代码,因此您甚至不必下载链接。 只需复制即可。

If you happen to write console utilities, I hope you find this useful.

如果您碰巧编写了控制台实用程序,希望对您有用。

翻译自: https://www.codeproject.com/Tips/5255878/A-Console-Progress-Bar-in-Csharp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值