DataBinding and updating on non UI thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark R. Dawson

    DataBinding and updating on non UI thread

    Hi all,
    I may be missing something with how databinding works but I have bound a
    datasource to a control and everything is great, the control updates to
    reflect the state of my datasource when I update the datasource - awesome,
    but I have an issue with updating from a different thread.

    Here is my datasource, a person class that raises the PropertyChanged event:

    class Person : INotifyProperty Changed
    {
    private string _name;

    public Person(string name)
    {
    _name = name;
    }

    public string Name
    {
    get
    {
    return _name;
    }
    set
    {
    _name = value;

    if (PropertyChange d != null)
    {
    PropertyChanged (this, new
    PropertyChanged EventArgs("Name "));
    }
    }
    }

    #region INotifyProperty Changed Members

    public event PropertyChanged EventHandler PropertyChanged ;

    #endregion
    }

    and here is my form code which has a button and a label, the label displays
    the persons name:

    private Person _person;

    private BindingSource _bsPerson;

    private void ThreadForm_Load (object sender, EventArgs e)
    {
    //Create person and bind person instance name property to label
    _person = new Person("Bob");

    _bsPerson = new BindingSource() ;
    _bsPerson.DataS ource = _person;

    Binding personBinding = new Binding("Text", _bsPerson, "Name");
    label1.DataBind ings.Add(person Binding);
    }

    private void btnUpdateInUITh read_Click(obje ct sender, EventArgs e)
    {
    _person.Name = "Frank";
    }

    However, now I want to update my datasource in a different thread than
    the thread which created the control. If I update my datasource, i.e. change
    the persons name I get a threading error WHICH I WOULD EXPECT to get since
    the databinding is updating the control in a different thread that the one
    which created it. For example:

    private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
    {
    //Create a thread which will attempt to update the
    //label which displays the person name
    Thread t = new Thread(delegate ()
    {
    _person.Name = "Jim";
    });

    t.Start();
    }

    My question is how can I get the binding to update in the correct thread,
    I could clear the controls binding collection and re-add them after the
    source has been updated but that seems to defeat the point of binding in the
    first place, I don't want to be thinking about what thread has been used to
    update my datasources. Why can't the binding be smart enough to detect a
    cross thread operation and call invoke.

    I could also as the Microsoft documentation suggests put the updates in a
    list and then process them in the Main UI thread, but to me that seems a hack
    some part of my object model will be responsible for updating the model
    automatically, I don't want to be exposing this kind of code.

    It seems to me like this would be a common occurance, hopefully I am just
    missing something.

    Thanks
    Mark
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: DataBinding and updating on non UI thread

    Mark,

    You are going to have to pay attention to where the change on the
    operation is going to take place. Basically what you need to do is on the
    control that is data bound to, call the InvokeRequired method. If it
    returns true, then you need to pass a delegate to the Invoke method on the
    control, along with any parameters that might be needed. The delegate you
    pass would be the method which will set the property with the appropriate
    value.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
    news:2930B471-DF4C-42E1-B4E7-80F797BB6202@mi crosoft.com...[color=blue]
    > Hi all,
    > I may be missing something with how databinding works but I have bound a
    > datasource to a control and everything is great, the control updates to
    > reflect the state of my datasource when I update the datasource - awesome,
    > but I have an issue with updating from a different thread.
    >
    > Here is my datasource, a person class that raises the PropertyChanged
    > event:
    >
    > class Person : INotifyProperty Changed
    > {
    > private string _name;
    >
    > public Person(string name)
    > {
    > _name = name;
    > }
    >
    > public string Name
    > {
    > get
    > {
    > return _name;
    > }
    > set
    > {
    > _name = value;
    >
    > if (PropertyChange d != null)
    > {
    > PropertyChanged (this, new
    > PropertyChanged EventArgs("Name "));
    > }
    > }
    > }
    >
    > #region INotifyProperty Changed Members
    >
    > public event PropertyChanged EventHandler PropertyChanged ;
    >
    > #endregion
    > }
    >
    > and here is my form code which has a button and a label, the label
    > displays
    > the persons name:
    >
    > private Person _person;
    >
    > private BindingSource _bsPerson;
    >
    > private void ThreadForm_Load (object sender, EventArgs e)
    > {
    > //Create person and bind person instance name property to label
    > _person = new Person("Bob");
    >
    > _bsPerson = new BindingSource() ;
    > _bsPerson.DataS ource = _person;
    >
    > Binding personBinding = new Binding("Text", _bsPerson, "Name");
    > label1.DataBind ings.Add(person Binding);
    > }
    >
    > private void btnUpdateInUITh read_Click(obje ct sender, EventArgs e)
    > {
    > _person.Name = "Frank";
    > }
    >
    > However, now I want to update my datasource in a different thread than
    > the thread which created the control. If I update my datasource, i.e.
    > change
    > the persons name I get a threading error WHICH I WOULD EXPECT to get since
    > the databinding is updating the control in a different thread that the one
    > which created it. For example:
    >
    > private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
    > {
    > //Create a thread which will attempt to update the
    > //label which displays the person name
    > Thread t = new Thread(delegate ()
    > {
    > _person.Name = "Jim";
    > });
    >
    > t.Start();
    > }
    >
    > My question is how can I get the binding to update in the correct thread,
    > I could clear the controls binding collection and re-add them after the
    > source has been updated but that seems to defeat the point of binding in
    > the
    > first place, I don't want to be thinking about what thread has been used
    > to
    > update my datasources. Why can't the binding be smart enough to detect a
    > cross thread operation and call invoke.
    >
    > I could also as the Microsoft documentation suggests put the updates in a
    > list and then process them in the Main UI thread, but to me that seems a
    > hack
    > some part of my object model will be responsible for updating the model
    > automatically, I don't want to be exposing this kind of code.
    >
    > It seems to me like this would be a common occurance, hopefully I am just
    > missing something.
    >
    > Thanks
    > Mark[/color]


    Comment

    • Mark R. Dawson

      #3
      Re: DataBinding and updating on non UI thread

      Hi Nicholas,
      thanks for your reply. I would think that the reason someone would have
      been using databinding in the first place is so that they don't have to write
      extra code to update their controls. I am not sure why MS would not have
      just have the databinding stack figure out that it needs to call Invoke
      internally.

      Where exactly would you put the check to InvokeRequired in the chain from
      the data source being updated to the CurrencyManager pushing the value to the
      bound control that you mentioned, is there some event that you have to
      intercept?

      Thanks
      Mark.

      Mark.

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      [color=blue]
      > Mark,
      >
      > You are going to have to pay attention to where the change on the
      > operation is going to take place. Basically what you need to do is on the
      > control that is data bound to, call the InvokeRequired method. If it
      > returns true, then you need to pass a delegate to the Invoke method on the
      > control, along with any parameters that might be needed. The delegate you
      > pass would be the method which will set the property with the appropriate
      > value.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
      > news:2930B471-DF4C-42E1-B4E7-80F797BB6202@mi crosoft.com...[color=green]
      > > Hi all,
      > > I may be missing something with how databinding works but I have bound a
      > > datasource to a control and everything is great, the control updates to
      > > reflect the state of my datasource when I update the datasource - awesome,
      > > but I have an issue with updating from a different thread.
      > >
      > > Here is my datasource, a person class that raises the PropertyChanged
      > > event:
      > >
      > > class Person : INotifyProperty Changed
      > > {
      > > private string _name;
      > >
      > > public Person(string name)
      > > {
      > > _name = name;
      > > }
      > >
      > > public string Name
      > > {
      > > get
      > > {
      > > return _name;
      > > }
      > > set
      > > {
      > > _name = value;
      > >
      > > if (PropertyChange d != null)
      > > {
      > > PropertyChanged (this, new
      > > PropertyChanged EventArgs("Name "));
      > > }
      > > }
      > > }
      > >
      > > #region INotifyProperty Changed Members
      > >
      > > public event PropertyChanged EventHandler PropertyChanged ;
      > >
      > > #endregion
      > > }
      > >
      > > and here is my form code which has a button and a label, the label
      > > displays
      > > the persons name:
      > >
      > > private Person _person;
      > >
      > > private BindingSource _bsPerson;
      > >
      > > private void ThreadForm_Load (object sender, EventArgs e)
      > > {
      > > //Create person and bind person instance name property to label
      > > _person = new Person("Bob");
      > >
      > > _bsPerson = new BindingSource() ;
      > > _bsPerson.DataS ource = _person;
      > >
      > > Binding personBinding = new Binding("Text", _bsPerson, "Name");
      > > label1.DataBind ings.Add(person Binding);
      > > }
      > >
      > > private void btnUpdateInUITh read_Click(obje ct sender, EventArgs e)
      > > {
      > > _person.Name = "Frank";
      > > }
      > >
      > > However, now I want to update my datasource in a different thread than
      > > the thread which created the control. If I update my datasource, i.e.
      > > change
      > > the persons name I get a threading error WHICH I WOULD EXPECT to get since
      > > the databinding is updating the control in a different thread that the one
      > > which created it. For example:
      > >
      > > private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
      > > {
      > > //Create a thread which will attempt to update the
      > > //label which displays the person name
      > > Thread t = new Thread(delegate ()
      > > {
      > > _person.Name = "Jim";
      > > });
      > >
      > > t.Start();
      > > }
      > >
      > > My question is how can I get the binding to update in the correct thread,
      > > I could clear the controls binding collection and re-add them after the
      > > source has been updated but that seems to defeat the point of binding in
      > > the
      > > first place, I don't want to be thinking about what thread has been used
      > > to
      > > update my datasources. Why can't the binding be smart enough to detect a
      > > cross thread operation and call invoke.
      > >
      > > I could also as the Microsoft documentation suggests put the updates in a
      > > list and then process them in the Main UI thread, but to me that seems a
      > > hack
      > > some part of my object model will be responsible for updating the model
      > > automatically, I don't want to be exposing this kind of code.
      > >
      > > It seems to me like this would be a common occurance, hopefully I am just
      > > missing something.
      > >
      > > Thanks
      > > Mark[/color]
      >
      >
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: DataBinding and updating on non UI thread

        Mark,

        I don't know why you are doing this in the binding class. Rather, it is
        usually the programmer of the control to make sure that any bound data
        sources are changed on the appropriate thread.

        So, your control should not have to do anything, rather, the thread that
        is changing the data source should be aware of this, and act accordingly (in
        other words, the thread making the change should call InvokeRequired and
        Invoke).

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
        news:A2C44B11-1DB7-4C80-B4BE-E9A0350D61F4@mi crosoft.com...[color=blue]
        > Hi Nicholas,
        > thanks for your reply. I would think that the reason someone would have
        > been using databinding in the first place is so that they don't have to
        > write
        > extra code to update their controls. I am not sure why MS would not have
        > just have the databinding stack figure out that it needs to call Invoke
        > internally.
        >
        > Where exactly would you put the check to InvokeRequired in the chain from
        > the data source being updated to the CurrencyManager pushing the value to
        > the
        > bound control that you mentioned, is there some event that you have to
        > intercept?
        >
        > Thanks
        > Mark.
        >
        > Mark.
        >
        > "Nicholas Paldino [.NET/C# MVP]" wrote:
        >[color=green]
        >> Mark,
        >>
        >> You are going to have to pay attention to where the change on the
        >> operation is going to take place. Basically what you need to do is on
        >> the
        >> control that is data bound to, call the InvokeRequired method. If it
        >> returns true, then you need to pass a delegate to the Invoke method on
        >> the
        >> control, along with any parameters that might be needed. The delegate
        >> you
        >> pass would be the method which will set the property with the appropriate
        >> value.
        >>
        >> Hope this helps.
        >>
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >> "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
        >> news:2930B471-DF4C-42E1-B4E7-80F797BB6202@mi crosoft.com...[color=darkred]
        >> > Hi all,
        >> > I may be missing something with how databinding works but I have bound
        >> > a
        >> > datasource to a control and everything is great, the control updates
        >> > to
        >> > reflect the state of my datasource when I update the datasource -
        >> > awesome,
        >> > but I have an issue with updating from a different thread.
        >> >
        >> > Here is my datasource, a person class that raises the PropertyChanged
        >> > event:
        >> >
        >> > class Person : INotifyProperty Changed
        >> > {
        >> > private string _name;
        >> >
        >> > public Person(string name)
        >> > {
        >> > _name = name;
        >> > }
        >> >
        >> > public string Name
        >> > {
        >> > get
        >> > {
        >> > return _name;
        >> > }
        >> > set
        >> > {
        >> > _name = value;
        >> >
        >> > if (PropertyChange d != null)
        >> > {
        >> > PropertyChanged (this, new
        >> > PropertyChanged EventArgs("Name "));
        >> > }
        >> > }
        >> > }
        >> >
        >> > #region INotifyProperty Changed Members
        >> >
        >> > public event PropertyChanged EventHandler PropertyChanged ;
        >> >
        >> > #endregion
        >> > }
        >> >
        >> > and here is my form code which has a button and a label, the label
        >> > displays
        >> > the persons name:
        >> >
        >> > private Person _person;
        >> >
        >> > private BindingSource _bsPerson;
        >> >
        >> > private void ThreadForm_Load (object sender, EventArgs e)
        >> > {
        >> > //Create person and bind person instance name property to
        >> > label
        >> > _person = new Person("Bob");
        >> >
        >> > _bsPerson = new BindingSource() ;
        >> > _bsPerson.DataS ource = _person;
        >> >
        >> > Binding personBinding = new Binding("Text", _bsPerson,
        >> > "Name");
        >> > label1.DataBind ings.Add(person Binding);
        >> > }
        >> >
        >> > private void btnUpdateInUITh read_Click(obje ct sender, EventArgs
        >> > e)
        >> > {
        >> > _person.Name = "Frank";
        >> > }
        >> >
        >> > However, now I want to update my datasource in a different thread
        >> > than
        >> > the thread which created the control. If I update my datasource, i.e.
        >> > change
        >> > the persons name I get a threading error WHICH I WOULD EXPECT to get
        >> > since
        >> > the databinding is updating the control in a different thread that the
        >> > one
        >> > which created it. For example:
        >> >
        >> > private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
        >> > {
        >> > //Create a thread which will attempt to update the
        >> > //label which displays the person name
        >> > Thread t = new Thread(delegate ()
        >> > {
        >> > _person.Name = "Jim";
        >> > });
        >> >
        >> > t.Start();
        >> > }
        >> >
        >> > My question is how can I get the binding to update in the correct
        >> > thread,
        >> > I could clear the controls binding collection and re-add them after the
        >> > source has been updated but that seems to defeat the point of binding
        >> > in
        >> > the
        >> > first place, I don't want to be thinking about what thread has been
        >> > used
        >> > to
        >> > update my datasources. Why can't the binding be smart enough to detect
        >> > a
        >> > cross thread operation and call invoke.
        >> >
        >> > I could also as the Microsoft documentation suggests put the updates
        >> > in a
        >> > list and then process them in the Main UI thread, but to me that seems
        >> > a
        >> > hack
        >> > some part of my object model will be responsible for updating the model
        >> > automatically, I don't want to be exposing this kind of code.
        >> >
        >> > It seems to me like this would be a common occurance, hopefully I am
        >> > just
        >> > missing something.
        >> >
        >> > Thanks
        >> > Mark[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Mark R. Dawson

          #5
          Re: DataBinding and updating on non UI thread

          here is a simple example of what I don't understand how to handle, if I have
          a person class and it has a Save method that takes 30 seconds to save, the
          save should happen on a seperate thread so the UI remains responsive. The
          person instance has been bound to a label and when it is saved internally the
          state of the object is changed so the binding refreshes, however this is now
          in the thread that saved the object not the main UI thread, the person class
          knows nothing about what it is bound to in the UI, so how would you deal with
          this case?

          You could do:

          1. before calling save break all the datasource links to the person i.e.
          label1.DataSour ce = null then reenable them after the save has completed.

          but I don't like that, since I don't want to have to think about this, if I
          do I might as we ll write the update manually without binding.

          Thanks
          Mark


          "Nicholas Paldino [.NET/C# MVP]" wrote:
          [color=blue]
          > Mark,
          >
          > I don't know why you are doing this in the binding class. Rather, it is
          > usually the programmer of the control to make sure that any bound data
          > sources are changed on the appropriate thread.
          >
          > So, your control should not have to do anything, rather, the thread that
          > is changing the data source should be aware of this, and act accordingly (in
          > other words, the thread making the change should call InvokeRequired and
          > Invoke).
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
          > news:A2C44B11-1DB7-4C80-B4BE-E9A0350D61F4@mi crosoft.com...[color=green]
          > > Hi Nicholas,
          > > thanks for your reply. I would think that the reason someone would have
          > > been using databinding in the first place is so that they don't have to
          > > write
          > > extra code to update their controls. I am not sure why MS would not have
          > > just have the databinding stack figure out that it needs to call Invoke
          > > internally.
          > >
          > > Where exactly would you put the check to InvokeRequired in the chain from
          > > the data source being updated to the CurrencyManager pushing the value to
          > > the
          > > bound control that you mentioned, is there some event that you have to
          > > intercept?
          > >
          > > Thanks
          > > Mark.
          > >
          > > Mark.
          > >
          > > "Nicholas Paldino [.NET/C# MVP]" wrote:
          > >[color=darkred]
          > >> Mark,
          > >>
          > >> You are going to have to pay attention to where the change on the
          > >> operation is going to take place. Basically what you need to do is on
          > >> the
          > >> control that is data bound to, call the InvokeRequired method. If it
          > >> returns true, then you need to pass a delegate to the Invoke method on
          > >> the
          > >> control, along with any parameters that might be needed. The delegate
          > >> you
          > >> pass would be the method which will set the property with the appropriate
          > >> value.
          > >>
          > >> Hope this helps.
          > >>
          > >>
          > >> --
          > >> - Nicholas Paldino [.NET/C# MVP]
          > >> - mvp@spam.guard. caspershouse.co m
          > >>
          > >> "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
          > >> news:2930B471-DF4C-42E1-B4E7-80F797BB6202@mi crosoft.com...
          > >> > Hi all,
          > >> > I may be missing something with how databinding works but I have bound
          > >> > a
          > >> > datasource to a control and everything is great, the control updates
          > >> > to
          > >> > reflect the state of my datasource when I update the datasource -
          > >> > awesome,
          > >> > but I have an issue with updating from a different thread.
          > >> >
          > >> > Here is my datasource, a person class that raises the PropertyChanged
          > >> > event:
          > >> >
          > >> > class Person : INotifyProperty Changed
          > >> > {
          > >> > private string _name;
          > >> >
          > >> > public Person(string name)
          > >> > {
          > >> > _name = name;
          > >> > }
          > >> >
          > >> > public string Name
          > >> > {
          > >> > get
          > >> > {
          > >> > return _name;
          > >> > }
          > >> > set
          > >> > {
          > >> > _name = value;
          > >> >
          > >> > if (PropertyChange d != null)
          > >> > {
          > >> > PropertyChanged (this, new
          > >> > PropertyChanged EventArgs("Name "));
          > >> > }
          > >> > }
          > >> > }
          > >> >
          > >> > #region INotifyProperty Changed Members
          > >> >
          > >> > public event PropertyChanged EventHandler PropertyChanged ;
          > >> >
          > >> > #endregion
          > >> > }
          > >> >
          > >> > and here is my form code which has a button and a label, the label
          > >> > displays
          > >> > the persons name:
          > >> >
          > >> > private Person _person;
          > >> >
          > >> > private BindingSource _bsPerson;
          > >> >
          > >> > private void ThreadForm_Load (object sender, EventArgs e)
          > >> > {
          > >> > //Create person and bind person instance name property to
          > >> > label
          > >> > _person = new Person("Bob");
          > >> >
          > >> > _bsPerson = new BindingSource() ;
          > >> > _bsPerson.DataS ource = _person;
          > >> >
          > >> > Binding personBinding = new Binding("Text", _bsPerson,
          > >> > "Name");
          > >> > label1.DataBind ings.Add(person Binding);
          > >> > }
          > >> >
          > >> > private void btnUpdateInUITh read_Click(obje ct sender, EventArgs
          > >> > e)
          > >> > {
          > >> > _person.Name = "Frank";
          > >> > }
          > >> >
          > >> > However, now I want to update my datasource in a different thread
          > >> > than
          > >> > the thread which created the control. If I update my datasource, i.e.
          > >> > change
          > >> > the persons name I get a threading error WHICH I WOULD EXPECT to get
          > >> > since
          > >> > the databinding is updating the control in a different thread that the
          > >> > one
          > >> > which created it. For example:
          > >> >
          > >> > private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
          > >> > {
          > >> > //Create a thread which will attempt to update the
          > >> > //label which displays the person name
          > >> > Thread t = new Thread(delegate ()
          > >> > {
          > >> > _person.Name = "Jim";
          > >> > });
          > >> >
          > >> > t.Start();
          > >> > }
          > >> >
          > >> > My question is how can I get the binding to update in the correct
          > >> > thread,
          > >> > I could clear the controls binding collection and re-add them after the
          > >> > source has been updated but that seems to defeat the point of binding
          > >> > in
          > >> > the
          > >> > first place, I don't want to be thinking about what thread has been
          > >> > used
          > >> > to
          > >> > update my datasources. Why can't the binding be smart enough to detect
          > >> > a
          > >> > cross thread operation and call invoke.
          > >> >
          > >> > I could also as the Microsoft documentation suggests put the updates
          > >> > in a
          > >> > list and then process them in the Main UI thread, but to me that seems
          > >> > a
          > >> > hack
          > >> > some part of my object model will be responsible for updating the model
          > >> > automatically, I don't want to be exposing this kind of code.
          > >> >
          > >> > It seems to me like this would be a common occurance, hopefully I am
          > >> > just
          > >> > missing something.
          > >> >
          > >> > Thanks
          > >> > Mark
          > >>
          > >>
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • Nick Hounsome

            #6
            Re: DataBinding and updating on non UI thread

            There are 2 places to put the InvokeRequired stuff:

            1. In the non-GUI thread
            2. In a Person.OnProper tyChanged() method that you should use to fire the
            event. On... is a common (if somewhat unintuitive pattern) that MS uses to
            raise events and with C#2.0 INotifyProperty Changed it is really a no-brainer
            since otherwise you duplicate code whenever you have more than one bindable
            property.

            You might also want to consider the more general SynchronizingOb ject
            pattern (see System.Timers.T imer.Synchroniz ingObject amongst others) to
            specify the required object implementing ISynchronizeInv oke rather than
            explicitly referencing a control or form.

            "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
            news:A2C44B11-1DB7-4C80-B4BE-E9A0350D61F4@mi crosoft.com...[color=blue]
            > Hi Nicholas,
            > thanks for your reply. I would think that the reason someone would have
            > been using databinding in the first place is so that they don't have to
            > write
            > extra code to update their controls. I am not sure why MS would not have
            > just have the databinding stack figure out that it needs to call Invoke
            > internally.
            >
            > Where exactly would you put the check to InvokeRequired in the chain from
            > the data source being updated to the CurrencyManager pushing the value to
            > the
            > bound control that you mentioned, is there some event that you have to
            > intercept?
            >
            > Thanks
            > Mark.
            >
            > Mark.
            >
            > "Nicholas Paldino [.NET/C# MVP]" wrote:
            >[color=green]
            >> Mark,
            >>
            >> You are going to have to pay attention to where the change on the
            >> operation is going to take place. Basically what you need to do is on
            >> the
            >> control that is data bound to, call the InvokeRequired method. If it
            >> returns true, then you need to pass a delegate to the Invoke method on
            >> the
            >> control, along with any parameters that might be needed. The delegate
            >> you
            >> pass would be the method which will set the property with the appropriate
            >> value.
            >>
            >> Hope this helps.
            >>
            >>
            >> --
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>
            >> "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.com> wrote in message
            >> news:2930B471-DF4C-42E1-B4E7-80F797BB6202@mi crosoft.com...[color=darkred]
            >> > Hi all,
            >> > I may be missing something with how databinding works but I have bound
            >> > a
            >> > datasource to a control and everything is great, the control updates
            >> > to
            >> > reflect the state of my datasource when I update the datasource -
            >> > awesome,
            >> > but I have an issue with updating from a different thread.
            >> >
            >> > Here is my datasource, a person class that raises the PropertyChanged
            >> > event:
            >> >
            >> > class Person : INotifyProperty Changed
            >> > {
            >> > private string _name;
            >> >
            >> > public Person(string name)
            >> > {
            >> > _name = name;
            >> > }
            >> >
            >> > public string Name
            >> > {
            >> > get
            >> > {
            >> > return _name;
            >> > }
            >> > set
            >> > {
            >> > _name = value;
            >> >
            >> > if (PropertyChange d != null)
            >> > {
            >> > PropertyChanged (this, new
            >> > PropertyChanged EventArgs("Name "));
            >> > }
            >> > }
            >> > }
            >> >
            >> > #region INotifyProperty Changed Members
            >> >
            >> > public event PropertyChanged EventHandler PropertyChanged ;
            >> >
            >> > #endregion
            >> > }
            >> >
            >> > and here is my form code which has a button and a label, the label
            >> > displays
            >> > the persons name:
            >> >
            >> > private Person _person;
            >> >
            >> > private BindingSource _bsPerson;
            >> >
            >> > private void ThreadForm_Load (object sender, EventArgs e)
            >> > {
            >> > //Create person and bind person instance name property to
            >> > label
            >> > _person = new Person("Bob");
            >> >
            >> > _bsPerson = new BindingSource() ;
            >> > _bsPerson.DataS ource = _person;
            >> >
            >> > Binding personBinding = new Binding("Text", _bsPerson,
            >> > "Name");
            >> > label1.DataBind ings.Add(person Binding);
            >> > }
            >> >
            >> > private void btnUpdateInUITh read_Click(obje ct sender, EventArgs
            >> > e)
            >> > {
            >> > _person.Name = "Frank";
            >> > }
            >> >
            >> > However, now I want to update my datasource in a different thread
            >> > than
            >> > the thread which created the control. If I update my datasource, i.e.
            >> > change
            >> > the persons name I get a threading error WHICH I WOULD EXPECT to get
            >> > since
            >> > the databinding is updating the control in a different thread that the
            >> > one
            >> > which created it. For example:
            >> >
            >> > private void btnUpdateInNewT hread_Click(obj ect sender, EventArgs e)
            >> > {
            >> > //Create a thread which will attempt to update the
            >> > //label which displays the person name
            >> > Thread t = new Thread(delegate ()
            >> > {
            >> > _person.Name = "Jim";
            >> > });
            >> >
            >> > t.Start();
            >> > }
            >> >
            >> > My question is how can I get the binding to update in the correct
            >> > thread,
            >> > I could clear the controls binding collection and re-add them after the
            >> > source has been updated but that seems to defeat the point of binding
            >> > in
            >> > the
            >> > first place, I don't want to be thinking about what thread has been
            >> > used
            >> > to
            >> > update my datasources. Why can't the binding be smart enough to detect
            >> > a
            >> > cross thread operation and call invoke.
            >> >
            >> > I could also as the Microsoft documentation suggests put the updates
            >> > in a
            >> > list and then process them in the Main UI thread, but to me that seems
            >> > a
            >> > hack
            >> > some part of my object model will be responsible for updating the model
            >> > automatically, I don't want to be exposing this kind of code.
            >> >
            >> > It seems to me like this would be a common occurance, hopefully I am
            >> > just
            >> > missing something.
            >> >
            >> > Thanks
            >> > Mark[/color]
            >>
            >>
            >>[/color][/color]


            Comment

            Working...