【问题标题】:Progress bar with VB.NET Console Application带有 VB.NET 控制台应用程序的进度条
【发布时间】:2015-05-04 12:30:54
【问题描述】:

我已经编写了一个解析实用程序作为控制台应用程序,并且它运行得非常顺利。该实用程序读取分隔文件并根据用户值作为命令行参数将记录拆分为 2 个文件之一(好记录或坏记录)。

希望在解析时显示进度条或状态指示器以显示已完成的工作或剩余的工作。我可以在循环中轻松地在屏幕上写一个 <.> 但想给出一个 %。

谢谢!

【问题讨论】:

  • % 计算是您的问题还是在同一位置打印它的方式?

标签: vb.net progress-bar console-application


【解决方案1】:

这是一个如何计算完成百分比并将其输出到进度计数器的示例:

Option Strict On
Option Explicit On

Imports System.IO

Module Module1
    Sub Main()
        Dim filePath As String = "C:\StackOverflow\tabSeperatedFile.txt"
        Dim FileContents As String()


        Console.WriteLine("Reading file contents")
        Using fleStream As StreamReader = New StreamReader(IO.File.Open(filePath, FileMode.Open, FileAccess.Read))
            FileContents = fleStream.ReadToEnd.Split(CChar(vbTab))
        End Using

        Console.WriteLine("Sorting Entries")
        Dim TotalWork As Decimal = CDec(FileContents.Count)
        Dim currentLine As Decimal = 0D

        For Each entry As String In FileContents
            'Do something with the file contents

            currentLine += 1D
            Dim progress = CDec((currentLine / TotalWork) * 100)

            Console.SetCursorPosition(0I, Console.CursorTop)
            Console.Write(progress.ToString("00.00") & " %")
        Next

        Console.WriteLine()
        Console.WriteLine("Finished.")
        Console.ReadLine()

    End Sub
End Module

【讨论】:

    【解决方案2】:

    首先你必须知道你会期望多少行。 在您的循环中计算“intLineCount / 100 * intCurrentLine”

    int totalLines = 0 // "GetTotalLines"
    int currentLine = 0;
    foreach (line in Lines)
    {
      /// YOUR OPERATION
    
      currentLine ++;
      int progress = totalLines / 100 * currentLine;
    
      ///print out the result with the suggested method...
      ///!Caution: if there are many updates consider to update the output only if the value has changed or just every n loop by using the MOD operator or any other useful approach ;)
    }
    

    并使用 SetCursor 方法在循环中的相同位置打印结果 MSDN Console.SetCursorPosition

    VB.NET:

    Dim totalLines as Integer = 0
    Dim currentLine as integer = 0
    For Each line as string in Lines
         ' Your operation
    
         currentLine += 1I
         Dim Progress as integer = (currentLine / totalLines) * 100
    
        ' print out the result with the suggested method...
        ' !Caution: if there are many updates consider to update the output only if the value has changed or just every n loop by using the MOD operator or any other useful approach 
    
    Next
    

    【讨论】:

    • 我已将您的链接更改为英文版本,因为大多数读者不懂德语,并且我添加了与您的 C# 代码等效的 VB.NET,因为 OP 已标记此问题vb.net
    • @BrandonB 嗨-当然是我的错np
    • 很好的例子!我让它成功运行。谢谢!
    【解决方案3】:

    嗯,最简单的方法是经常更新progressBar变量, 例如:如果您的代码包含大约 100 行或可能是 100 个功能 在每个函数或某些代码行之后用百分比更新进度条变量:)

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      相关资源
      最近更新 更多