Paip. Improved performance --- end solution of loading 0.3 million large data volumes in ListBox

Source: Internet
Author: User

Paip. Improved performance --- end solution of loading 0.3 million large data volumes in ListBox

Author attilax, email: 1466519819@qq.com

After some hard work, The ListBox finally loaded 0.3 million pieces of data with a good performance solution ......

The result is as follows:
1 K, 0.6 s
1 W 5S
10 W, 48 s
In this way, we can estimate that 0.3 million of the data should be loaded in about 150 seconds. The frontend UI will not be stuck in the way of subsequent threads ............ High efficiency has been achieved,

The most important thing to improve performance is thread. Sleep (10 );
-------------------------

While (line! = NULL)
{
 
N ++;


If (N % 1000 = 0)
{
Console. writeline ("--" + N. tostring () + ":" + line );
Thread. Sleep (10 );

}

Thread. Sleep is too frequent, the UI is smoother, but the background thread efficiency is not high, and vice versa ......
So we use N % 1000 to let the background thread load one thousand data and switch to the UI thread. In this way, the loading efficiency of data threads and the smoothness of UI threads are both ...... Achieved good results ......

If you load 10 million data and then sleep it, it will be 10 W, 40 s, but the UI will get stuck ......

If sleep (1) is used for loading each piece of data, the efficiency will be reduced by 5 K, 10 ............

------------ Source code :----------

Int loadrecnum = 0;
Long curtime = 0;
// Long sleepspan
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)
{
Lastupdatesec = 0;
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;
 

Form1 fm = (form1) OBJ;
While (line! = NULL)
{

// Thread. Sleep (1 );
// Console. writeline ("--" + N. tostring () + ":" + line );
N ++;

// Write the lie to console window
If (N % 1000 = 0)
{
Console. writeline ("--" + N. tostring () + ":" + line );
Thread. Sleep (10 );

}

Add2list (line );
Updateconter4listbox ();

Int readnum = This. settingform. getreadnum ();
If (n> = readnum)
Break;
// Read the next line
Line = Sr. Readline ();

}
Updateconter4listboxlimidtl ();
// Close the file
Sr. Close ();

// Listboxendupdate ();
/// C45



}

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

 
}

}

Int lastupdatesec = 0;
/// <Summary>
/// Update per secs
/// </Summary>
Private void updateconter4listbox ()
{
// Loadrecnum ++;

Long nowtime = datetime. Now. ticks;
Float span = (float) (nowtime-curtime)/(float) 10000000 ));
Int spanint = (INT) span;
If (spanint <= lastupdatesec)
Return;
Else
Lastupdatesec = spanint;

Label9.invoke (New eventhandler (delegate
{
Label9.text = "in use:" + span. tostring () + "seconds ";

}));

Label8.invoke (New eventhandler (delegate
{
Label8.text = "loaded:" + loadrecnum. tostring ();

}));
}

Private void updateconter4listboxlimidtl ()
{
// Loadrecnum ++;

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 ";

}));

Label8.invoke (New eventhandler (delegate
{
Label8.text = "loaded:" + loadrecnum. tostring ();

}));
}

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 ++;
}));
}

}

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.