Windows Develop Bookmark and Share   
 index > Windows Forms General > INotifyPropertyChange performance issue
 

INotifyPropertyChange performance issue

Hi all,

I have a big form with 60 textbox control witch binded to a back-end class . I the class I implemented the INotifyPropertyChange interface and the binding is working fine.

The problem is , if I change on of the textboxes, the property_change events occures, and all the binded properties on the class are called. This affects my application performance and I don't find how to block this. The screen calls all the properties even if they didn't changed.

Is there away that the NotifyPropertychange update only the textbox that changed, or if one property changed in the class it updtes the corresponding textbox without calling to the the other class property.

Thanks in advance...

wasimf  Monday, April 14, 2008 1:30 PM

Hi wasimf,

I have written a sample to test your case, there is windows form in my demo, the form has two textboxes control, and bind it to a INotifyPropertyChanged derived class, I cannot recur your question, when only one textbox changed, the Property changed event will be fired only once, please check the code snippet below.

Code Snippet

INotifyPropertyChanged_Demo

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

DemoCustomer d1 = DemoCustomer.CreateNewCustomer();

ArrayList ar = new ArrayList();

ar.Add(d1);

textBox1.DataBindings.Add(new Binding("Text", ar[0], "CustomerName"));

DemoCustomer d2 = DemoCustomer.CreateNewCustomer();

ar.Add(d2);

textBox2.DataBindings.Add(new Binding("Text", ar[1], "CustomerName"));

}

}

// This class implements a simple customer type

// that implements the IPropertyChange interface.

public class DemoCustomer : INotifyPropertyChanged

{

string customerName = "";

public event PropertyChangedEventHandler PropertyChanged = new PropertyChangedEventHandler(Handle_Property_Changed);

private void NotifyPropertyChanged(String info)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(info));

}

}

private static void Handle_Property_Changed(object sender, PropertyChangedEventArgs e)

{

MessageBox.Show(e.PropertyName + " changed");

}

// The constructor is private to enforce the factory pattern.

private DemoCustomer()

{

customerName = "no data";

}

// This is the public factory method.

public static DemoCustomer CreateNewCustomer()

{

return new DemoCustomer();

}

public string CustomerName

{

get

{

return this.customerName;

}

set

{

if (value != this.customerName)

{

this.customerName = value;

NotifyPropertyChanged("customerName");

}

}

}

}

If you still cannot figure out the bug, I think you should post the detailed code here for troubleshooting.

For your information:

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

http://www.codeproject.com/KB/cs/PropertyNotifyPart1.aspx

http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Regards,

Xun

Xun Ye  Thursday, April 17, 2008 7:33 AM

Hi

can you post the code you used?

If you say effects the performance, what exactly do you mean?

frederikm  Monday, April 14, 2008 3:28 PM

Are you passing the property name, or null/empty, in the arg?

Also - are you binding directly to the object, or do you have something else (BindingSource, BindingList<T>, etc) in the pipeline?

Finally - you might get a different result if you go for separate FooChanged, BarChanged events - the binding framework will find these (for the Foo and Bar properties respectively), but it won't work through BindingList<T> - so a double-edged sword.

Marc

Marc Gravell  Tuesday, April 15, 2008 7:01 AM

Hi ,

I used BindingSource to bind between the control(say combobox) and the object property.

Why does the binding framwork case to call all the binded controls and rebind them, I just changed one property.

Thanks...

wasimf  Wednesday, April 16, 2008 6:42 AM

Hi wasimf,

I have written a sample to test your case, there is windows form in my demo, the form has two textboxes control, and bind it to a INotifyPropertyChanged derived class, I cannot recur your question, when only one textbox changed, the Property changed event will be fired only once, please check the code snippet below.

Code Snippet

INotifyPropertyChanged_Demo

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

DemoCustomer d1 = DemoCustomer.CreateNewCustomer();

ArrayList ar = new ArrayList();

ar.Add(d1);

textBox1.DataBindings.Add(new Binding("Text", ar[0], "CustomerName"));

DemoCustomer d2 = DemoCustomer.CreateNewCustomer();

ar.Add(d2);

textBox2.DataBindings.Add(new Binding("Text", ar[1], "CustomerName"));

}

}

// This class implements a simple customer type

// that implements the IPropertyChange interface.

public class DemoCustomer : INotifyPropertyChanged

{

string customerName = "";

public event PropertyChangedEventHandler PropertyChanged = new PropertyChangedEventHandler(Handle_Property_Changed);

private void NotifyPropertyChanged(String info)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(info));

}

}

private static void Handle_Property_Changed(object sender, PropertyChangedEventArgs e)

{

MessageBox.Show(e.PropertyName + " changed");

}

// The constructor is private to enforce the factory pattern.

private DemoCustomer()

{

customerName = "no data";

}

// This is the public factory method.

public static DemoCustomer CreateNewCustomer()

{

return new DemoCustomer();

}

public string CustomerName

{

get

{

return this.customerName;

}

set

{

if (value != this.customerName)

{

this.customerName = value;

NotifyPropertyChanged("customerName");

}

}

}

}

If you still cannot figure out the bug, I think you should post the detailed code here for troubleshooting.

For your information:

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

http://www.codeproject.com/KB/cs/PropertyNotifyPart1.aspx

http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Regards,

Xun

Xun Ye  Thursday, April 17, 2008 7:33 AM

You can use google to search for other answers

Custom Search

More Threads

• "File Open" Dialog Box
• WinForm Usercontrols communicating to each other
• how can i implement multithreading in this case
• Retrieve handle to Checkbox in CheckedListBox
• TextBox text selectable but not editable
• how to read my.application.log at runtime
• position of form
• delete xml file and create a new xml file with same name
• URGENT:System.Resources.ResXResourceWriter
• how to alter key values in app.config from code view