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

ycoder
  • 5
  • possible duplicate of [Progress Bar and Backgroundworker VB.Net](http://stackoverflow.com/questions/27565851/progress-bar-and-backgroundworker-vb-net) – Ňɏssa Pøngjǣrdenlarp Jan 21 '15 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 '15 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. – Ňɏssa Pøngjǣrdenlarp Jan 21 '15 at 18:56
  • so how can I report from a different class and using a backgroundworker? – ycoder Jan 21 '15 at 19:07
  • its similar to: http://stackoverflow.com/questions/15707156/c-sharp-updating-backgroundworker-progress-from-separate-class or http://stackoverflow.com/questions/14871238/report-progress-backgroundworker-from-different-class-c-sharp Does anyone how to solve this in vb.net? – ycoder Jan 21 '15 at 20:30
  • they are all the same - they all have accepted answers. so apply them to your case. – Ňɏssa Pøngjǣrdenlarp Jan 21 '15 at 21:31
  • But in a Different Language. Im searching for a solution in Vb.net. – ycoder Jan 22 '15 at 17:06

0 Answers0