关于listbox的加载问题 WinForm 就不会了,不过我以前用VB做时,是加载一条数据就把窗体刷新一下,不知道.net的WinForm支不支持这样 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 需要用到多线程。一个线程加载数据,一个线程填充ListBox。 liufuyahong() ( ) 信誉:100 2007-7-16 18:35:29 得分: 0 需要用到多线程。一个线程加载数据,一个线程填充ListBox。 怎么做? 如果是添加每一条的时间都很长,那可能是你添加的对象太大了,或是硬件问题,因为ListBox的add()方法的性能很高的,只是每添加一个都要重新绘制该控件,但数据量小的话,影响不大。(用BeginUpdate 和 EndUpdate可以解决重绘问题)你的最终目的是想提高程序的性能,对吗?ListBox(一般控件)的数据显示,一般的做法有:一是直接调用add(),如果这样做,性能就取决于每次添加的对象大小,对象小当然就快了。二是设置DataSource数据源,和方法一的性能差不多。三是先将ListBox的Visible设为fasle,填充完数据后再show出。当然,填充的操作要在较空闲的时候做好。再有一点要注意的,就是准备填充的数据一定要事先准备好,如果一边提取或转型,一边做添加,那性能肯定会受到很大影响。要程序性能,需要在实现中相互协调。希望能给你些思路。 winform 不会webform 做不到, 即使 ajax 也是全部加载完后显示 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; } 有关接口的问题!十万火急。。。 datagridview的checkbox的问题 C#中如何判断,耳机插孔事件。 实现检测端口是否打开的功能!请前辈们指教。 c# 正则表达式的写法 如何获取窗体中可看到的TextBox第一行的行号 s.s.o service 是什么技术 Winform中登录后,如何使webform不用再次登陆 C#怎么设置指定进程的HOOK 关于用API取指定句柄窗口里控件的内容 在tabcontrol上如果做触发 递归生成treeeview节点时,在第二层为何会产生如下错误:
需要用到多线程。一个线程加载数据,一个线程填充ListBox。
怎么做?
ListBox(一般控件)的数据显示,一般的做法有:
一是直接调用add(),如果这样做,性能就取决于每次添加的对象大小,对象小当然就快了。
二是设置DataSource数据源,和方法一的性能差不多。
三是先将ListBox的Visible设为fasle,填充完数据后再show出。当然,填充的操作要在较空闲的时候做好。
再有一点要注意的,就是准备填充的数据一定要事先准备好,如果一边提取或转型,一边做添加,那性能肯定会受到很大影响。要程序性能,需要在实现中相互协调。希望能给你些思路。
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;
}