作者Attilax , EMAIL:1466519819@qq.com
我想可能是数据量太大。同时需要测试下LISTBOX的性能极限……就使用默认的 this.listBox1.Items.Add(line);这种方式
结果如下:
1千行数据:1秒
1万行: 3.5秒
10万行: 40秒
对于性能,我的要求是3.5秒内应该加载进LISTBOX....
---------------主要源码如下:-----------
int loadRecNum = 0;
long curtime = 0;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Title = "请选择:";
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();
c45
loadRecNum++;
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
long nowtime = DateTime.Now.Ticks;
float span = (float)(((float)(nowtime - curtime)) / ((float)10000000));
int spanint = (int)span;
label9.Invoke(new EventHandler(delegate
{
label9.Text = "用 时:" + span.ToString() + "秒";
}));
}
));
t.Name = " --start txt2list thread";
t.IsBackground = true;
t.Start(this);
//String txt = filex.read(pathYZM, "gbk");
//String[] lines = txt.Split("\r\n".ToCharArray());
//accFilter4ihush af = new accFilter4ihush();
//foreach (String line in lines)
//{
// if(af.filterOK(line))
// this.listBox1.Items.Add(line);
// //this.listView1.Items.Add(item);
//}
}
}
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 = "已经加载:" + loadRecNum.ToString();
// }));
//long nowtime= DateTime.Now.Ticks;
//float span = (float)( ((float)( nowtime - curtime))/((float)10000000) );
//int spanint =(int) span;
//label9.Invoke(new EventHandler(delegate
//{
// label9.Text = "用 时:" + spanint.ToString() + "秒";
//}));
}
//this.listView1.Items.Add(item);
}