0

due to cleaning up my code a tried to update the progressbar from another class which is called by a backgroundworker. How can I accomplish this goal?

I tried:

    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

formerly my code was working, when all tasks are done in the same 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

Does anyone have an idea?

Thanks

7
  • possible duplicate of Progress Bar and Backgroundworker VB.Net Jan 21, 2015 at 18:21
  • Sorry, but your example is not using a separate class which reports progress. Please have a look at the first code. This must be solved through a different way.
    – ycoder
    Jan 21, 2015 at 18:52
  • forms are classes (it says so at the top of each one: Public Class FormX), so the link is especially relevant. Doing the work in one form instance and reporting it on another is no different than doing the work in a custom class and reporting to a form. You are using implicit form instance which doesnt help. Jan 21, 2015 at 18:56
  • so how can I report from a different class and using a backgroundworker?
    – ycoder
    Jan 21, 2015 at 19:07
  • its similar to: stackoverflow.com/questions/15707156/… or stackoverflow.com/questions/14871238/… Does anyone how to solve this in vb.net?
    – ycoder
    Jan 21, 2015 at 20:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.