paip..net listbox效能極限

來源:互聯網
上載者:User

paip..net listbox效能極限

作者Attilax ,  EMAIL:1466519819@qq.com 


因為有個三十萬行,6.5M的跑號器資料進行匯入然後顯示到LISTBOX……但是程式崩潰。。

我想可能是資料量太大。同時需要測試下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);
           
        }

        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.