DataBinding and updating on non UI thread

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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 said:
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]
- (e-mail address removed)

Mark R. Dawson said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Mark R. Dawson said:
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 said:
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]
- (e-mail address removed)

Mark R. Dawson said:
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
 
G

Guest

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 said:
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]
- (e-mail address removed)

Mark R. Dawson said:
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 said:
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]
- (e-mail address removed)

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
 
N

Nick Hounsome

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 said:
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 said:
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]
- (e-mail address removed)

Mark R. Dawson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top