WinForm 就不会了,不过我以前用VB做时,是加载一条数据就把窗体刷新一下,不知道.net的WinForm支不支持这样

解决方案 »

  1.   

    需要用到多线程。一个线程加载数据,一个线程填充ListBox。
      

  2.   

    liufuyahong() ( ) 信誉:100  2007-7-16 18:35:29  得分: 0  
     
     
       
    需要用到多线程。一个线程加载数据,一个线程填充ListBox。  
     
    怎么做?
      

  3.   

    如果是添加每一条的时间都很长,那可能是你添加的对象太大了,或是硬件问题,因为ListBox的add()方法的性能很高的,只是每添加一个都要重新绘制该控件,但数据量小的话,影响不大。(用BeginUpdate 和 EndUpdate可以解决重绘问题)你的最终目的是想提高程序的性能,对吗?
    ListBox(一般控件)的数据显示,一般的做法有:
    一是直接调用add(),如果这样做,性能就取决于每次添加的对象大小,对象小当然就快了。
    二是设置DataSource数据源,和方法一的性能差不多。
    三是先将ListBox的Visible设为fasle,填充完数据后再show出。当然,填充的操作要在较空闲的时候做好。
    再有一点要注意的,就是准备填充的数据一定要事先准备好,如果一边提取或转型,一边做添加,那性能肯定会受到很大影响。要程序性能,需要在实现中相互协调。希望能给你些思路。
      

  4.   

    winform 不会webform 做不到, 即使 ajax 也是全部加载完后显示
      

  5.   

    bool Finished = false;
            bool GetFinished = false;        delegate void SetTextCallback(string text);        
            private void button1_Click(object sender, EventArgs e)
            {            
                string CurItem = "";
                object Lock = new object();            
                this.listBox1.Items.Clear();
                Finished = false;
                button1.Enabled = false;
                GetFinished = false;            System.Threading.Thread threadGetItem=new System.Threading.Thread(
                    delegate(){                    
                        for (int i = 0; i < 200; )
                        {
                            lock (Lock)
                            {
                                if (!GetFinished)
                                {
                                    CurItem = string.Format("Item {0}", i); //获取Item
                                    GetFinished = true;
                                    i++;
                                }
                                System.Threading.Thread.Sleep(10);
                            }
                        }
                        Finished = true;
                    }
                );            System.Threading.Thread threadDispItem = new System.Threading.Thread(
                    delegate()
                    {
                        while (!Finished) 
                        {
                            lock (Lock)
                            {
                                if (GetFinished)
                                {
                                    SetText(CurItem);
                                    GetFinished = false;
                                }
                                else
                                {
                                    System.Threading.Thread.Sleep(10);
                                }
                            }
                            
                        }
                    }
                );            threadGetItem.Start();
                threadDispItem.Start();
            }        private void SetText(string text)
            {            
                if (this.listBox1.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    this.listBox1.Items.Add(text);                
                }            if (!this.button1.InvokeRequired&&Finished)
                    this.button1.Enabled = true;
            }