2015-05-04 73 views
0

我已经写了一个解析实用程序作为控制台应用程序,并让它工作得很顺利。该实用程序读取分隔文件并根据用户值作为命令行参数将记录拆分为2个文件(好记录或坏记录)中的一个。VB.NET控制台应用程序的进度条

寻找一个进度条或状态指示器来显示分析时执行的工作或剩余的工作。我可以在循环内的屏幕上轻松地编写一个<。>但希望给出%。

谢谢!

+0

是%计算的问题还是打印在同一位置上的方式? – Cadburry

回答

1

首先你必须知道你会有多少行。 在你的循环计算 “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打印在循环相同posititon结果:

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 
+0

因为大多数读者不知道德语,所以我已经将您的链接更改为英文版,并且我已经添加了与您的C#代码相同的VB.NET,因为OP已将这个问题标记为“vb.net”。 –

+0

@BrandonB嗨 - 确定我的错np – Cadburry

+0

很好的例子!我已经成功地工作了。 谢谢! –

0

那么最简单的方法是经常更新progressBar变量,例如: 例如:如果您的代码包含大约100行或可能是100个功能 ,每个功能或代码更新进度变量的某些线与百分比:)

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