G
Guest
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 : INotifyPropertyChanged
{
private string _name;
public Person(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs("Name"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler 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.DataSource = _person;
Binding personBinding = new Binding("Text", _bsPerson, "Name");
label1.DataBindings.Add(personBinding);
}
private void btnUpdateInUIThread_Click(object 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 btnUpdateInNewThread_Click(object 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
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 : INotifyPropertyChanged
{
private string _name;
public Person(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs("Name"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler 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.DataSource = _person;
Binding personBinding = new Binding("Text", _bsPerson, "Name");
label1.DataBindings.Add(personBinding);
}
private void btnUpdateInUIThread_Click(object 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 btnUpdateInNewThread_Click(object 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