Often a listview control in C# needs to be populated with large amount of data (bulky operations, for example, adding 1000 items). For every item added in the Listview, the control will redraw itself, thus greatly reduce the overall performance.
However, using the two methods BeginUpdate() and EndUpdate() while performing these bulk operations gives significant advantage. A call to BeginUpdate() can be made before adding/deleting/clearing items. This will stop any paint messages being sent or processed. Once the operation is done, the EndUpdate() can then be called.
However, using the two methods BeginUpdate() and EndUpdate() while performing these bulk operations gives significant advantage. A call to BeginUpdate() can be made before adding/deleting/clearing items. This will stop any paint messages being sent or processed. Once the operation is done, the EndUpdate() can then be called.
I have logged the time taken of the above code (i.e. adding 1000 items in a ListView for each button click) with and without the BeginUpdate/EndUpdate method calls using a test application.
The use BeginUpdate() and EndUpdate populates the control with a constant duration and much better performance. Amazing, isn't it?
Here's a simple source code example for using this technique with a ListBox:
ListBox1.Items.BeginUpdate; for i := 1 to 10000 do ListBox1.Items.Add('abcd'); ListBox1.Items.EndUpdate;
To give you an idea of the improvement, we timed the code above (your PC may be faster or slower):
without the BeginUpdate/EndUpdate lines: 4.3 seconds...
with BeginUpdate/EndUpdate: 0.1 seconds, that's 43 times faster!
That’s it!!…..Happy Programming...
No comments:
Post a Comment