George Vaisey
George Vaisey

Reputation: 149

Progress bar with VB.NET Console Application

I've written a parsing utility as a Console Application and have it working pretty smoothly. The utility reads delimited files and based on a user value as a command line arguments splits the record to one of 2 files (good records or bad records).

Looking to do a progress bar or status indicator to show work performed or remaining work while parsing. I could easily write a <.> across the screen within the loop but would like to give a %.

Thanks!

Upvotes: 0

Views: 4089

Answers (3)

Here is an example of how to calculate the percentage complete and output it in a progress counter:

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

Upvotes: 2

Cadburry
Cadburry

Reputation: 1864

1rst you have to know how many lines you will expect. In your loop calculate "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 ;)
}

and print the result on the same posititon in your loop by using the SetCursor method 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

Upvotes: 1

Raki
Raki

Reputation: 535

Well The easiest way is to update the progressBar variable often, Ex: if your code consist of around 100 lines or may be 100 functionality after each function or certain lines of code update progressbar variable with percentage :)

Upvotes: 0

Related Questions