Results 1 to 30 of 30

Thread: Correct way to use the BackgroundWorker

Hybrid View

  1. #1
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Correct way to use the BackgroundWorker

    OMG, that easy and still so far for me. Thank you. Can you please tell me why that first parameter should be zero and not 100?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Correct way to use the BackgroundWorker

    Quote Originally Posted by Grand View Post
    Can you please tell me why that first parameter should be zero and not 100?
    The first parameter should be whatever you want it to be. If you are actually going to use the value to provide feedback to the user then it should accurately represent the current progress. If you're not going to use it for anything in the ProgressChanged event handler then what's the point of using anything other than zero?

    I'm not convinced that you're doing the right thing here though. You are passing a DataTable and you seem to be indicating that the progress is at 100%, i.e. the background work is complete. If you are trying to pass data back to the UI thread when the work is complete then you should not be calling ReportProgress and handling ProgressChanged. Instead, you should be setting the e.Result property and letting the DoWork event handler complete. You then handle the RunWorkerCompleted event and get the data back from the e.Result property there. If this DataTable is the result of the backgroundwork then that is how it should be handled. ReportProgress/ProgressChanged is just to keep the user updated along the way. That might involve just the numerical progress or it might be a batch of data that you have processed so far but if you are only transferring data in a single block at the end, don't do it using ReportProgress/ProgressChanged.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Correct way to use the BackgroundWorker

    Quote Originally Posted by jmcilhinney View Post
    If you are trying to pass data back to the UI thread when the work is complete then you should not be calling ReportProgress and handling ProgressChanged.
    Instead, you should be setting the e.Result property and letting the DoWork event handler complete. You then handle the RunWorkerCompleted event and get the data back from the e.Result property there. If this DataTable is the result of the backgroundwork then that is how it should be handled.
    Spot on. That is exactly what I wanted to do. Would you also please tell me how exactly I set e.result property? And how I bring back the table so I can bind it with the datagridview?
    Thanks.

    I tried this but the table is not returend here:

    Code:
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    
            Dim MyDataTable As DataTable = DirectCast(e.Result, DataTable)
            DataGridView2.DataSource = MyDataTable
            SpinnerForm.Close() 'Close the form with spinner
    
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Correct way to use the BackgroundWorker

    Quote Originally Posted by Grand View Post
    Spot on. That is exactly what I wanted to do. Would you also please tell me how exactly I set e.result property? And how I bring back the table so I can bind it with the datagridview?
    Thanks.

    I tried this but the table is not returend here:

    Code:
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    
            Dim MyDataTable As DataTable = DirectCast(e.Result, DataTable)
            DataGridView2.DataSource = MyDataTable
            SpinnerForm.Close() 'Close the form with spinner
    
        End Sub
    That part is right but you didn't put the DataTable there in the first place to get it back again. I told you exactly what to do:
    Instead, you should be setting the e.Result property and letting the DoWork event handler complete.
    How can you be asking how to set a property? You're already doing it in code you already have. The code you just posted sets the DataSource property of a DataGridView. Why should setting any other property be any different?
    Last edited by jmcilhinney; Feb 12th, 2019 at 06:31 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width