Paip. Improve Performance ---.net listbox use BeginUpdate and EndUpdate

Source: Internet
Author: User

Use the default this. listBox1.Items. Add (line );

 


The result is as follows:
1 million rows of data: 1 second
Row 3: 10 thousand seconds
0.1 million rows: 40 seconds

 


Query Information, using BeginUpdate and EndUpdate can improve performance.
Therefore, BeginUpdate and EndUpdate are used.


The result is as follows:


The result is as follows:
1 million lines of data: 0.5 seconds
Row 3: 4 seconds
Row 0.1 million: 44 seconds

 


Conclusion:
--------
When BeginUpdate and EndUpdate are used in thousands of rows, the performance can be improved by no factor ......
However, when there are hundreds of thousands and hundreds of thousands of data, it is even 10 thousand slower than the default method ......

 

 

 


--------------- Main source code :-----------

 

 


Int loadRecNum = 0;
Long curtime = 0;
Private void button#click (object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog ();
OpenFileDlg. Title = "Please select :";
OpenFileDlg. Filter = "*. * | *.*";
OpenFileDlg. ShowDialog ();
If (openFileDlg. CheckFileExists)
{
If (openFileDlg. FileName. Equals (""))
Return;
String pathYZM = openFileDlg. FileName;

 


// C452308 add txt2list
Thread t = new Thread (new ParameterizedThreadStart (
Delegate (object obj)
{
LoadRecNum = 0;
Curtime = DateTime. Now. Ticks;


// C45 big txt


// Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader (pathYZM );


// Read the first line of text
String line = sr. ReadLine ();


// Continue to read until you reach end of file
Int n = 0;
// ListBox1.Visible = false;


ListBox1.Invoke (new EventHandler (delegate
{
This. listBox1.BeginUpdate ();
}));

Form1 fm = (Form1) obj;
While (line! = Null)
{
// Thread. Sleep (5 );
// Console. WriteLine ("--" + n. ToString () + ":" + line );
N ++;

// Write the lie to console window
If (n % 1000 = 0)
{
Console. WriteLine ("--" + n. ToString () + ":" + line );
// ListBox1.Invoke (new EventHandler (delegate
//{
// ListBox1.EndUpdate ();
/// This. listBox1.BeginUpdate ();
//}));

}


Add2List (line );


Int readNum = this. settingForm. getReadNum ();
If (n> = readNum)
Break;
// Read the next line
Line = sr. ReadLine ();
// If (line = null)
// ListBox1.Invoke (new EventHandler (delegate
//{
// ListBox1.EndUpdate ();

//}));


}
// ListBox1.EndUpdate ();
// ListBox1.Visible = true;
// Close the file
Sr. Close ();


ListboxEndUpdate ();
/// C45

 


LoadRecNum ++;
Label8.Invoke (new EventHandler (delegate
{
Label8.Text = "loaded:" + loadRecNum. ToString ();


}));


Long nowtime = DateTime. Now. Ticks;
Float span = (float) (nowtime-curtime)/(float) 10000000 ));
Int spanint = (int) span;
Label9.Invoke (new EventHandler (delegate
{
Label9.Text = "in use:" + span. ToString () + "seconds ";


}));

}


));
T. Name = "-- start txt2list thread ";
T. IsBackground = true;
T. Start (this );

 



 
}

 

 


}


Private void ListboxEndUpdate ()
{
ListBox1.Invoke (new EventHandler (delegate
{
ListBox1.EndUpdate ();


}));
}
Int nc45 = 0;
Public void add2List (string line)
{

AccFilter4ihush af = new accFilter4ihush ();


If (af. filterOK (line ))
{
LoadRecNum ++;
// BeginInvoke
ListBox1.Invoke (new EventHandler (delegate
{

// Thread. Sleep (5000 );
This. listBox1.Items. Add (line );
// Console. WriteLine ("-- add2List:" + nc45.ToString () + ":" + line );
Nc45 ++;
}));


// Label8.Invoke (new EventHandler (delegate
//{
// Label8.Text = "loaded:" + loadRecNum. ToString ();

//}));


// Long nowtime = DateTime. Now. Ticks;
// Float span = (float) (nowtime-curtime)/(float) 10000000 ));
// Int spanint = (int) span;
// Label9.Invoke (new EventHandler (delegate
//{
// Label9.Text = "in use:" + spanint. ToString () + "seconds ";


//}));


}



// This. listView1.Items. Add (item );

}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.