简体   繁体   English

使用BackgroundWorker或调用方法(VB.net)在MainUI上更新进度栏

[英]Updating Progressbar on the MainUI Using either BackgroundWorker or invoke method(VB.net)

Good day, 美好的一天,

I need some help please. 我需要一些帮助。 I have tried multiple methods to update a progressbar from a backgroundworker and the invoke method with no avail. 我尝试了多种方法来更新后台工作人员的进度条和invoke方法,但无济于事。 I have all my code in a module which i want to access from the main ui. 我将所有代码都存储在要从主ui访问的模块中。 I start the backgroundworker from the mainform and call my procedures for the module which needs to report back to the mainforms ui (Progressbar). 我从主窗体启动backgroundworker,并为需要向主窗体ui返回报告的模块调用我的程序ui(进度栏)。 PBar is a public intreger that counts as the progress count for the Progress bar. PBar是一个公共整数,它作为进度栏的进度计数。 BackGroundWorker: BackGroundWorker:

 Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
    PastelGRNS(0)
    PastelRTS(0)
    PastelINVS(0)
    PastelCRNS(0)
End Sub

Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
    Me.PB1.Value = CInt(PBar)
End Sub

Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, _
        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
        Handles BackgroundWorker.RunWorkerCompleted
    If e.Cancelled = True Then
        Alert(Me, "Canceled", "The process has been canceled.", MessageBoxButtons.OK)
    ElseIf e.Error IsNot Nothing Then
        Alert(Me, "Error", e.Error.Message, MessageBoxButtons.OK)
    Else
        Alert(Me, "Done", "All work has been completed.", MessageBoxButtons.OK)
    End If
End Sub

The Invoke ProgressBar: 调用进度条:

  Delegate Sub SetProgress(ByVal pbar1 As Integer) 'Your delegate..
  Public Sub ChangePB(pbar1 As Integer)
    If PB1.InvokeRequired Then 
        Dim d As New SetProgress(AddressOf ChangePB)
        Me.Invoke(d, New Object() {pbar1})

    Else
         PB1.Value = pbar1
       End If


End Sub

Snipet of code to update Backgroundworker progress: 代码片段以更新Backgroundworker进度:

    PBar = ((cnt / ReadCnt) * 100) * 0.25
                PBar = Math.Round(PBar, 0)
                MainMenu.BackgroundWorker.ReportProgress(CInt(PBar))

my module has for processors which run i need to update my progress bar acording to the percentage ie PBar = ((cnt / ReadCnt) * 100) * 0.25 (count 25% of the full bar) 我的模块具有运行的处理器,我需要根据百分比更新进度条,即PBar =((cnt / ReadCnt)* 100)* 0.25(占整个条形的25%)

I'm not sure why this isn't working. 我不确定为什么这行不通。 The pattern is pretty simple so I suggest you break it down and find out where the issue is. 模式非常简单,所以我建议您将其分解并找出问题所在。

Avoid using a global variable to hold the percentage, this should just be passed in when you call ReportProgress . 避免使用全局变量来保存百分比,这应该在调用ReportProgress时传入。

Here is a fully working sample: 这是一个完整的示例:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.WorkerReportsProgress = True
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    For i As Integer = 1 To 100
        BackgroundWorker1.ReportProgress(i)
        System.Threading.Thread.Sleep(10)
    Next
End Sub

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

Edit: You should not be directly updating a Progressbar in a Form from a Module. 编辑:您不应该直接从模块中更新窗体中的进度栏​​。 If you are then your design is wrong (you are not adhering to the Single responsibility principle). 如果是这样,则您的设计是错误的(您未遵循单一责任原则)。

You should do something along the lines of raising an Event that reports the progress, the subscriber than then decide what to do with this information = Thumbs up for code re-use. 您应该按照引发事件的方式执行一些操作,以报告进度,然后由订阅者决定如何处理此信息=赞许代码可重复使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM