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 5 12476
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]
- mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.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 : 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
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: 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] - mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message news:29**********************************@microsof t.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 : 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
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]
- mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com... 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:
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] - mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message news:29**********************************@microsof t.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 : 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
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.DataSource = 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: 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] - mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message news:A2**********************************@microsof t.com... 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:
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] - mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message news:29**********************************@microsof t.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 : 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
There are 2 places to put the InvokeRequired stuff:
1. In the non-GUI thread
2. In a Person.OnPropertyChanged() 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 INotifyPropertyChanged 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 SynchronizingObject
pattern (see System.Timers.Timer.SynchronizingObject amongst others) to
specify the required object implementing ISynchronizeInvoke rather than
explicitly referencing a control or form.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com... 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:
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] - mv*@spam.guard.caspershouse.com
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message news:29**********************************@microsof t.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 : 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephan Steiner |
last post by:
Hi
I'm using several DataTables in my program which are updated periodically.
At the same I have those tables bound to DataGrids in my GUI. So far I've
been doing all the processing in the same...
|
by: Luis Esteban Valencia |
last post by:
Hi,
the following code displays a search result in a datagrid and is working
without problems. But I think the way I have done is not correct especially
databind(). could anyone help me to...
|
by: Neil Steventon |
last post by:
Hi ,
I have started developing a database application , I say start as at this rate I see no finish.
I have a windows form which is bound to mydataset table "Companys" and this works great...
|
by: el_sid |
last post by:
Our developers have experienced a problem with updating Web References in
Visual Studio.NET 2003.
Normally, when a web service class (.asmx) is created, updating the Web
Reference will...
|
by: Joey Chömpff |
last post by:
L.S.,
Hello is there a way to implement 2-way databinding without using the
datasources from dotnet 2.0. Why would you ask? Now that's simple.
I've created an object model with BusinessObjects...
|
by: =?Utf-8?B?R29rdWw=?= |
last post by:
I am struck up with a problem and want anyone here to help me out. I am a
beginner in .NET trying to learng DataBinding concepts. I have binded 4 text
boxes with a dataset but when I say...
|
by: Vlado Jasovic |
last post by:
Hello,
I'm using typed dataset for databinding to windows controls and I'm having
some problems.
I'm trying to move all business logic to datatable column_changing events
and the problem that...
|
by: Jason Wolf |
last post by:
I have an object which runs in a sperate thread, from the main thread, and
while running it updates a public property.
In my main form I defined databinding on the property and I would like to
see...
|
by: David Smith |
last post by:
I have an app that shows the time it takes to execute a search, much like SQL Management Studio, but the app behaves as though the System.Windows.Forms.Timer gets blocked while binding the results to...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |