VB.NET 控制台应用程序的进度条
Progress bar with VB.NET Console Application
我已经编写了一个解析实用程序作为控制台应用程序,并且它运行得非常顺利。该实用程序读取带分隔符的文件,并根据用户值作为命令行参数将记录拆分为 2 个文件之一(好记录或坏记录)。
希望做一个进度条或状态指示器来显示解析时已执行的工作或剩余的工作。我可以很容易地在循环内的屏幕上写一个 <.> 但我想给一个 %.
谢谢!
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:
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
嗯,最简单的方法是经常更新 progressBar 变量,
例如:如果您的代码包含大约 100 行或可能包含 100 个功能
在每个函数或某些代码行之后用百分比更新进度条变量:)
以下是如何计算完成百分比并将其输出到进度计数器中的示例:
Option Strict On
Option Explicit On
Imports System.IO
Module Module1
Sub Main()
Dim filePath As String = "C:\Whosebug\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 个文件之一(好记录或坏记录)。
希望做一个进度条或状态指示器来显示解析时已执行的工作或剩余的工作。我可以很容易地在循环内的屏幕上写一个 <.> 但我想给一个 %.
谢谢!
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:
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
嗯,最简单的方法是经常更新 progressBar 变量, 例如:如果您的代码包含大约 100 行或可能包含 100 个功能 在每个函数或某些代码行之后用百分比更新进度条变量:)
以下是如何计算完成百分比并将其输出到进度计数器中的示例:
Option Strict On
Option Explicit On
Imports System.IO
Module Module1
Sub Main()
Dim filePath As String = "C:\Whosebug\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