?
Solved

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

Posted on 2006-04-01
12
Medium Priority
?
1,241 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:fruhj
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16361012
It's difficult to tell what the problem is from your description.

Bob
0
 
LVL 12

Author Comment

by:fruhj
ID: 16364535
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16364558
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Author Comment

by:fruhj
ID: 16365097
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.

0
 
LVL 12

Author Comment

by:fruhj
ID: 16367236
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16370437
Where/when are the controls data-bound?

Bob
0
 
LVL 12

Author Comment

by:fruhj
ID: 16377364
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.

0
 
LVL 12

Author Comment

by:fruhj
ID: 16377415
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?


0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16380688
Have you ever used Reflector?

Bob
0
 
LVL 12

Author Comment

by:fruhj
ID: 16386924
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16391048
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
0
 
LVL 12

Author Comment

by:fruhj
ID: 16391238
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question