Databindings and BackgroundWorker component

I am having an issue whereby the controls that I have bound to my custom business object are not reflecting changes to the source object after it has been persisted to the database. The business object is declared as a private member in the Form instance, and after InitializeComponent runs I call a method that creates all the necessary bindings and adds them to the appropriate controls. When the Save button is clicked, the BackgroundWorker is instantiated and the event handlers for the DoWork, ReportProgress, and WorkCompleted events are wired up, then the BackgroundWorker.RunWorkerAsync() method is called, which then passes the business object to the middle tier (static class that just passes it down to the data access tier), the data access class saves it to the database and sets a few properties on the business object (database-generated fields). So the same object is worked on in the data access layer as I am passing the reference to the business object down from the UI layer to the data access layer. However the controls never reflect the changes to the object. I am working with the .Net 2.0 framework, btw.

Are there special considerations/techniques for working with an object that is bound to form controls on another thread (i.e. the BackgroundWorker thread) Are the controls not being updated because the changes to the business object were made by another thread other than the one they were created on  

My custom business object does implement INotifyPropertyChanged interface, and the set accessor on each of the object's properties fires the PropertyChanged event if the new value is different from the current value.


Answer this question

Databindings and BackgroundWorker component

  • maag008

    I have a backgroundworker and I am calling my dataadapter.fill method in DoWork method. I call the bindingsource.resetbindings(false) in backgroundworker_workercompleted event. This works when i run my app from VS. However, if I install this app at client side, the app freezes. This happens exactly at the bindingsource.resetbindings(false) statement. Any ideas Thanks for your time.
  • Subodh Chettri

    Thanks for the reply--I simply used the BindingSource control as an intermediary to my object datasource and called BindingSource.ResetBindings() in the BackgroundWorker.WorkCompleted event handler.

  • Tailor

    I did the .resetbindings (after reading this post) and it works, however I'm concerned that it's not the "right" way to do this. I think Mark mentioned that It's not good to move the tableadapter.fill(..) command into the backgroundworker_DoWork sub.

    How would that get accomplished then

    If dataadapter.fill(dataset) fires off some UI events, is there a way to fill a dataset from a dataadapter without fireing off the UI



  • Venkateswararao

    Updating databound objects or updating the datasource of databound objects on a different (non-UI) thread is not supported. That is why the controls are not being updated. You'll need to Control.Invoke (or BeginInvoke) a delegate that performs the updates to your databound objects/datasource.
     
    -mark< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    Program Manager

    Microsoft

    This post is provided “as-is”

  • Databindings and BackgroundWorker component