VB.Net - 从后台工作人员更新进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15345155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:44:37  来源:igfitidea点击:

VB.Net - Updating progress bar from background worker

vb.netvisual-studio-2010progress-barbackgroundworker

提问by J_D

I am trying to build a log parser in VB.Net that will take IIS logs and insert them into a database. Having never really made a full out desktop application, I'm hitting a number of stumbling blocks, so please forgive me if my questions a very uninformed; I'm learning to walk while running.

我正在尝试在 VB.Net 中构建一个日志解析器,它将获取 IIS 日志并将它们插入到数据库中。我从来没有真正制作过一个完整的桌面应用程序,我遇到了许多绊脚石,所以如果我的问题非常无知,请原谅我;我正在学习边跑步边走路。

The code that I'm working with looks like this:

我正在使用的代码如下所示:

 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 opened it starts to read the log file and updates a textbox with all of the rows currently read, but I also want it to update a progress bar in my main thread called crunchingProgress.

因此该函数可以工作,当这个窗口打开时,它开始读取日志文件并使用当前读取的所有行更新文本框,但我也希望它更新我的主线程中名为 crunchingProgress 的进度条。

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by w.brian

This should do the trick:

这应该可以解决问题:

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

回答by Steve

You don't set the BackgroundWorker to report progress (WorkerReportsProgress)

您没有设置 BackgroundWorker 来报告进度(WorkerReportsProgress

BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()

Now your BackgroundWorker1_ProgressChangedwill be called in the context of the UI Thread and you can set the progressbar value

现在您BackgroundWorker1_ProgressChanged将在 UI 线程的上下文中被调用,您可以设置进度条值

Of course, when you call ReportProgress to raise the ProgressChanged event, you need to pass the percentage of work done so far.

当然,当您调用 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