Results 1 to 7 of 7

Thread: [RESOLVED] [2005] background worker

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Resolved [RESOLVED] [2005] background worker

    hi all,

    is it possible to use backgroundworker when loading database data / filling datatables?

    i did the ff: codes below but it doesn't work right... myDataTable is not being filled and the progressbar doesn't do anything.

    Code:
    
    Private Sub vo01_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.BackgroundWorker1.RunWorkerAsync()
    End Sub
    
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Me.myTableAdapter.Fill(Me.DataSet.myDataTable)
    End Sub
    
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
    End Sub
    
    please help... thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: [2005] background worker

    Your ProgressBar is never going to do anything because you never call the ReportProgress method so the ProgressChanged event is never raised. Even if you wanted to that would do no good though, because you can't get any feedback during the Fill method anyway. The table is empty before and populated after and there's no way to know where you are at in between. See the link below for an example of getting progress feedback from a BGW.

    http://vbforums.com/showthread.php?t=471889

    As for your DataTable not being filled, how do you know? How are you checking that? Also, what value does the Fill function return? That's the number of rows retrieved, so if that's zero it means that your query didn't find any matching rows. In that case your code is fine, but you're either using the wrong query or querying the wrong data.
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] background worker

    Quote Originally Posted by jmcilhinney
    Your ProgressBar is never going to do anything because you never call the ReportProgress method so the ProgressChanged event is never raised. Even if you wanted to that would do no good though, because you can't get any feedback during the Fill method anyway. The table is empty before and populated after and there's no way to know where you are at in between. See the link below for an example of getting progress feedback from a BGW.
    i see... i guess instead of using a progress bar i think i'm just gonna use some sort of animation to let the user know that the application is doing something.

    Quote Originally Posted by jmcilhinney
    As for your DataTable not being filled, how do you know? How are you checking that? Also, what value does the Fill function return? That's the number of rows retrieved, so if that's zero it means that your query didn't find any matching rows. In that case your code is fine, but you're either using the wrong query or querying the wrong data.
    what i did is i added codes to disable the form at form_load and then do the filling and enable the form at the backgroundworker's runworkercompleted event... but my form's txtboxes, comboboxes etc remains to be empty until i click on the next record button.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: [2005] background worker

    If you can click the Next Record button and get data then I would suggest that your DataTable must be being populated. If it wasn't then how would you be able to see any data at all? I asked you in my previous post what value your Fill method returned and you have chosen not to answer. If you want our help then I suggest you provide the information we need to help when we ask for it.
    i guess instead of using a progress bar i think i'm just gonna use some sort of animation to let the user know that the application is doing something.
    You can still use a ProgressBar for that, with its Style property set to Marquee.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] background worker

    hi,

    i tried to do what you said by using these codes..

    Code:
    Dim intNo As Int32
    intNo = Me.ableAdapter.Fill(Me.DataSet.Table)
    Debug.WriteLine(intNo)
    it returned 10.

    the datatable is being filled with no problems like what you said.
    but when i check the binding navigator the BindingNavigatorPositionItem says 0 of 0 which makes me think its not going to the first record.

    so i tried to experiment with it... on the BackgroundWorker1_RunWorkerCompleted i tried putting in different lines of codes like..
    1. Me.CustomerBindingSource.ResetBindings(True) -- works fine.
    2. Me.CustomerBindingSource.Position = 1 -- will work as long as its not 0.
    3. Me.CustomerBindingSource.MoveNext and Me.CustomerBindingSource.MoveLast are both working.

    when putting any of the codes above, it populates the textboxes, comboboxes etc. with the data... not sure why the TableAdapter.Fill is not enough and why i need to put these codes for it to populate my controls.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: [2005] background worker

    My guess would be because you are populating the DataTable outside the UI thread. You cannot access members of a control outside the UI thread, and having your data changes propagate to the UI would require that. As such I'm guessing that the BindingSource is smart enough to not even try. You're doing the righ thing by calling ResetBindings in the RunWorkerCompleted event handler. The reason setting the Position to zero wouldn't work is that it is already zero, so there's no change so no event will be raised.
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] background worker

    i see... well this is good enough for me.

    thanks for the info.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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