[英]how to faster scroll a listview to last item in listview in Winform in c#
我在winform中有一个列表视图,其中有一个列表视图,以便在收到新消息时滚动到列表中的最后一项。 我的问题是,当客户端中的消息发送得如此之快时,我的系统运行缓慢且一段时间没有响应,并且鼠标箭头的形状变为了滚轮。
范例范例:
for (int i = 1; i < 10000; i++) {
listView1.BeginUpdate();
listView1.Items.Add(a);
listView1.EnsureVisible(listView1.Items.Count - 1);
listView1.EndUpdate();
}
真实案例我的客户
当收到新消息时,此函数从线程调用。 我的Clint代码很大,我只显示此代码,因为此代码用于在列表视图中显示消息。
当我们注释滚动行时,当我们滚动到最后一条消息并且消息到达12,000时,系统性能会很好,这会降低速度。
private void HandleNewMsg(string msg)
{
if(chkAppend.Checked)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new AppendTextDelegate(HandleNewMsg), new object[] { msg });
}
else
{
// call here to parse the message
ShowMessage(ParseMessage(msg));
}
}
}
和显示消息方法
private void ShowMessage(List<String>MsgReceived){
listview1.BeginUpdate();
foreach(var i in MsgReceived)
{
listview1.Items.Add(i);
}
listView1.EnsureVisible(listView1.Items.Count - 1);
listview1.EndUpdate();
}
而ParseMessage是
private List<String> ParseMessage(String Message){
List<String> msg=new List<String>();
msg.Add(Message.Substring(0, 13));// title such as msg received
msg.Add("Msg Type " + Message.Substring(41+13 , 4));
msg.Add("p Code " + Message.Substring(65 +13, 2));
msg.Add("*************");
// and other data in it .
return msg;
}
如何使其不挂起并滚动到“上一封邮件”?
您应该阅读BeginUpdate
和EndUpdate
的手册。
为了正确使用它们,您应该按照以下方式编写:
listView1.BeginUpdate();
for (int i = 1; i < 10000; i++)
{
listView1.Items.Add(a);
}
listView1.EndUpdate();
listView1.EnsureVisible(listView1.Items.Count - 1);
这应添加10000个项目,然后滚动到最后一个项目。
在BeginUpdate
和EndUpdate
, ListView
不会呈现新添加的项目,因此在添加大量项目时不会滞后。
添加并渲染后,然后滚动到最后一个。
您所做的就是多余地渲染添加的项目,然后确保其可见性-再次导致渲染的原因。 这就是您的应用程序开始滞后的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.