vb.net 2.0 backgroundworker - how do I finalize the loading of a form when the .fill methods are in the backgroundworker?

I'm trying to make the loading of my app more responsive.

So far I have the background worker starting the fill methods that used to be in form load.
My problem is that once the worker is done, I don't know how to get the form to its default state.

Private Sub BackgroundWorker1_RunWorkerCompleted(....) Handles BackgroundWorker1.RunWorkerCompleted
        Me.Refresh()
        Me.TBL_IRP_SUPPLEMENTSBindingNavigator.Refresh()
End Sub

Form loads - displays a bunch of databound text boxes with nothing in them.
also displays the navigation control, with all arrows dimmed and record 0 of 0

After the RunWorkerCompleted event fires, the move next and move last arrows 'light up' (turn from a non-clickable gray to a clickable blue)

However the current record is still 0 of 0, and all the fields are blank until I click the move next arrow.

What can I call so that the form looks like it would if I had left the dataset.fill events in form_Load?
LVL 12
fruhjAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
It's difficult to tell what the problem is from your description.

Bob
fruhjAuthor Commented:
Hey Bob,

  What's the easiest way to communicate?  I can write more, post screenshots, and/or post a movie of the behavior.

  I'm willing and able to do whatever I can to make it easier for you.

  In a nutshell, I just want this form to look and behave "normally" with the added benefit of having the data load in a background process so the interface doesn't freeze up.  I've got it mostly working except when the background worker finishes, the form doesn't display any data.
 - Jack
Bob LearnedCommented:
1) What does the <Move next> arrow do?
2) How are you displaying the current record?
3) What is the context for the binding source?

Bob
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

fruhjAuthor Commented:
I'm on a client site and away from my development environment, I'll answer what I can from memory, I may need to fill in the gaps tonight...

1) What does the <Move next> arrow do?
  arrow is greyed out initially.
  is colored blue after the background worker completes.
  after the background worker is completed, the move next arrow works as it should - displaying the next record.
  I'll confirm tonight if the 'first' press of movenext displays record #1 or record #2

2) How are you displaying the current record?
  When the previous and next buttons are clicked, the standard VS2005 "under the hood" code handles the loading & display of the records

3) What is the context for the binding source?
   Not sure - by context, what do you mean? I can tell you the relations of the autogenerated objects tonight.

fruhjAuthor Commented:
Hi Bob, some additional information for you here.

When the form loads, the navigation controls are greyed out.
The background worker executes the tableadapter.fill methods
when the worker is finsiedh I refresh the BindingNavigator
then the Right navigation button changes from grey to blue.
the fields are all still blank
Clicking next record brings up the second record, fields have second record data.



Here's the code I have:

Public Class mainform

  Private Sub TBL_IRP_SUPPLEMENTSBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBL_IRP_SUPPLEMENTSBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.TBL_IRP_SUPPLEMENTSBindingSource.EndEdit()
        Me.TBL_IRP_SUPPLEMENTSTableAdapter.Update(Me.GigafarmerDataSet.TBL_IRP_SUPPLEMENTS)
    End Sub

    Private Sub mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        'FIRST 2 LINES COMMENTED FOR SIMPLICITY OF EE POSTING....
        'Me.TBL_IRPSUP_ACTIONCODESTableAdapter.Fill(Me.GigafarmerDataSet.TBL_IRPSUP_ACTIONCODES)
        'Me.TBL_IRPSUP_FUELCODESTableAdapter.Fill(Me.GigafarmerDataSet.TBL_IRPSUP_FUELCODES)
        Me.TBL_IRP_SUPPLEMENTSTableAdapter.Fill(Me.GigafarmerDataSet.TBL_IRP_SUPPLEMENTS)
    End Sub


    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.TBL_IRP_SUPPLEMENTSBindingNavigator.Refresh()
        Me.Refresh()
        ' PROBLEM HERE - ALL THE TEXT FIELDS ON MY FORM DO NOT DISPLAY ANYTHING AT THIS POINT.
        '(IF I CLICK NEXT RECORD, THEN THEY DO.)
    End Sub

end class

Thanks Bob for the help.

Please let me know if I can send you a pizza or if you have a wishlist on Amazon
Bob LearnedCommented:
Where/when are the controls data-bound?

Bob
fruhjAuthor Commented:
I'm not entirely sure.
This is a VS 2005 drag and drop winform.

The forms' Designer.vb code seems to show databindings like this one:
 Me.CreatedByTextBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.TBL_IRP_SUPPLEMENTSBindingSource, "CreatedBy", True))

Unlike the old Visual Studio 2003, (where a few more lines of code were necessary) it seems that in Visual Studio, if you drag and drop a table form your "data sources" window - everything works with zero hand coding.
If I look at what happens by default in a new project, I'd say the databinding occurs with a tableadapter.fill(dataset) method - since thats all the code I see.

fruhjAuthor Commented:
I did some additional searching on the internet and found a possible solution in a MS forum...

It seems to work, yet someone had indicated that it might not be good practice to move the tableadapter.fill into the backgroundworker_DoWork sub.

Anyhow here's the code that (appears to) work:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.TBL_IRP_SUPPLEMENTSBindingNavigator.Refresh()
        Me.TBL_IRP_SUPPLEMENTSBindingSource.ResetBindings(False)
        Me.Refresh()
End Sub

The only difference between this procedure and the one I posted above is the addition of the line:
Me.TBL_IRP_SUPPLEMENTSBindingSource.ResetBindings(False)

So now I've got it working, and out of curiosity, would like to know the "right" way to do it.
I'd also like to figure out how to know what tableadapter.fill does, and why the MVP on the MS forum felt it included UI code.

Any thoughts?


Bob LearnedCommented:
Have you ever used Reflector?

Bob
fruhjAuthor Commented:
I haven't used reflector, though your post prompted me to do a search and find it,

Looks similar to the object browser. I did notice the dependancy feature.
I tried using it for the .fill method, but it doesn't appear to use anything that would invoke any UI.

Do you have any thoughts on why it would be bad to use .fill in a background worker? I'm stuck here - one one hand, I don't think that fires off any UI stuff,  but on the other hand - SOMETHING is responsible for populating all the controls data initially, and .fill is the most likely culprit.

- Jack
Bob LearnedCommented:
My thoughts are about the timing of things.  Maybe a little detective work might help.  You need to find out where the data-bindings for the controls occur, and see if they are bound at the wrong time--before the asych background process is completed.

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fruhjAuthor Commented:
Sounds like a good course of action.

I'll have a poke around and see if I can find anything under the hood that would be happening in form_load that I'm not aware of.
- Jack
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.