I have a base class implementing INotifyPropertyChanged:
protected void OnNotifyChanged(string pName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(pName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
I have a derived class with a property Latitude like so:
private double latitude;
public double Latitude
{
get { return latitude; }
set { latitude = value; OnNotifyChanged("Latitude"); }
}
My derived class also has a method Fly that manipulates Latitude.
I also have a Form with a TextBox bound to Latitude of my derived class:
txtLat.DataBindings.Clear();
txtLat.DataBindings.Add("Text", bindSrc, "Latitude");
A thread is used to kick off Fly like so:
Thread tFly = new Thread(f.Fly);
tFly.IsBackground = true;
tFly.Start();
When Latitude changes, an exception is thrown:
DataBinding cannot find a row in the list that is suitable for all bindings.
This seems to be an odd issue with thread affinity. Ultimately, the code is trying to do the update from a non-UI thread – I’m unclear why it isn’t just displaying the cross-thread exception, though – I wonder whether this is actually a catch-all exception handler. If I remove the
BindingSource(and bind directly to the object, which is valid) you do get a cross-thread exception (which I expected).Personally, I would be inclined to handle this manually, i.e. subscribe to the event with a method that does an
Invoketo the UI thread and updates theTextmanually. However, I’m just checking if some previous cross-threaded binding code might help…Here’s an example using
Invoke:Here’s an example using a (modified) version of some old threading code of mine: