简体   繁体   中英

DataBinding to Textbox performance in C#.NET

I am working in a very high performance WinForms application, I get some data from a server and show these results in textboxes on my GUI. Would it be better, performance wise, to bind these text boxes to some variables in my code, or to just have in my code something like textbox1.text = data? Or it is just the same

When you're filling some view with data, performance isn't your main problem (as a rule).

Bindings are convenient from the perspective of development and further maintenance. They don't allow you to forget to put value back to data source after user had edit the value. Also, they tightly coupled with data validation. In fact, advanced binding engines, like WPF's one, makes data binding the only painless way to work with data in GUI.

Binding engine in WinForms is simpler (from the point of features it has), than in WPF, but it is still better, than textbox1.text = data .

It doesn't really matter. What would matter is the way you are fetching the data. If you have some slow operations like database queries you could execute them in a background thread, possibly using a BackgroundWorker to avoid freezing the main UI thread while this operation runs.

I'm not sure exactly what you are outputting, but this might be helpful for performance considerations:

Iteratively appending data/text to something like a textbox or string (eg in the case of logging error messages with something like textBox.Text += "message") is an O(n 2 ) operation, meaning the larger the output gets, the more dramatic a slow down you will see .

Additionally, in my experiments of iteratively adding text for output,

textBox.Text += "message";

is about 3200 times slower than appending directly to a string and then doing (at the end of the iterations)

textBox.Text = theString;

Using

textBox.AppendText("message");

was even slower (about 9600 times).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM