2015-05-04 40 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 
相關問題