Question Progressbar in textfile retreval?

fenhopi

Member
Joined
Jul 8, 2010
Messages
6
Programming Experience
Beginner
Hey,
I'm writing a program that retrieves certain lines from a textfile. This textfile is rather large, so is it possible to have a progressbar that shows the progress when it retrieves?
 
If work takes some time you should do it in background thread, BackgroundWorker is an easy solution that also supports progress notification. To know progress you must know how many lines there is, so you must read all lines first, else you can only show unknown progress indication using the Marquee/Continuous style (see ProgressBar.Style Property). If the file is just a few MBs (which is a HUGE text file) I'd just load it all into memory and start processing the lines while giving accurate progress. Loading a MBs size text file into memory is a split second operation (I got 64ms with a 5MB/35000 lines file). Here is a basic sample code:
VB.NET:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim lines() As String = IO.File.ReadAllLines("path")
    For i As Integer = 0 To lines.Length - 1
        Dim line As String = lines(i)
        '... process line ...
        Dim progress As Integer = i * 100 \ lines.Length
        Me.BackgroundWorker1.ReportProgress(progress)
    Next
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
 
Back
Top