vb.net class backgroundworker reportprogress

时间:2015-01-21 18:18:43

标签: vb.net class progress-bar backgroundworker

由于清理我的代码,我试图从另一个由后台工作者调用的类更新进度条。 我怎样才能实现这个目标?

我试过了:

    Public Class Form1
    'backgroundworker and progressbar are located in Form1

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

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
        End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim asyncwork As New calc
        asyncwork.calculate()
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
End Class
Public Class calc
    Public Sub calculate()
        Form1.BackgroundWorker1.ReportProgress(100)
    End Sub
End Class

以前我的代码正在运行,当所有任务都在同一个类中完成时:

 Public Class Form1
    'backgroundworker and progressbar are located in Form1

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

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    calclocal()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        calclocal()
    End Sub

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

End Class

有没有人有想法?

由于

0 个答案:

没有答案