VB.Net - Updating the progress bar from a background worker

I am trying to create a log parser in VB.Net that will accept IIS logs and insert them into the database. I have never made a full-fledged desktop application, I click a few stumbling blocks, so please forgive me if my questions are very uninformed; I learn to walk while working.

The code I'm working with is as follows:

 Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim logfile = "C:\ex111124.log"
    Dim FileLength As Long = New System.IO.FileInfo(logfile).Length
    logFileLabel.Text = logfile

    Dim objReader As New System.IO.StreamReader(logfile)
    Do While objReader.Peek() <> -1
        OngoingLog.AppendText(objReader.ReadLine)
        'BackgroundWorker1.ReportProgress(e.percentProgress)
        Loop()
    objReader.Close()
    objReader = Nothing

End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    'Me.crunchingProgress.Value = e.ProgressPercentage
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Close()
End Sub

So the function works when this window is open, it starts reading the log file and updates the text box with all the lines currently read, but I also want it to update the progress bar in my main thread called crunchingProgress.

Any help would be greatly appreciated.

+2
2

:

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Invoke(Sub()
        Me.crunchingProgress.Value = e.ProgressPercentage
    End Sub)
End Sub
+2

BackgroundWorker (WorkerReportsProgress)

BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()

BackgroundWorker1_ProgressChanged , progressbar

, ReportProgress ProgressChanged, .

Using objReader As New System.IO.StreamReader(logfile)
    Do While objReader.Peek() <> -1
        Dim line = objReader.ReadLine()
        OngoingLog.AppendText(line)
        Dim pct = Convert.ToInt32((100 * line.Length) / FileLength )
        BackgroundWorker1.ReportProgress(pct)
    Loop
End Using
+2

Source: https://habr.com/ru/post/1568314/


All Articles