本文介绍了与ListBox的显示/绘图对话缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序使用实体框架提取一小组很小的结果...大约需要3秒钟才能完成?为什么会这样?
My application uses entity framework to pull in a small tiny set of results... It takes about 3 seconds for it to do it? Why might this be?
Start.cs
...
private void projectToolStripMenuItem_Click(object sender, System.EventArgs e)
{
NewProject newProjectForm = new NewProject();
newProjectForm.ShowDialog(); // It seems to take about 3 or 4 seconds to actually get to this :S
}
...
NewProject.cs
public partial class NewProject : Form
{
private EFProjectTypeRepository projectTypeRepository;
public NewProject()
{
projectTypeRepository = new EFProjectTypeRepository();
InitializeComponent();
ListBoxProjectTypes();
}
public void ListBoxProjectTypes()
{
DateTime then = DateTime.Now;
// PreLoadedResources.projectTypes is a preloaded resource which takes split seconds to load.
ListBoxProjectType.Items.AddRange(PreLoadedResources.projectTypes.Select(item => (object)item.Title).ToArray()); // If I comment this line out, the dialogue is almost instant @ timeTaken {00:00:00.0010019}
DateTime now = DateTime.Now;
TimeSpan timeTaken = now.Subtract(then);
}
}
:
timeTaken {00:00:02.4261369} System.TimeSpan
当我第二次显示对话时,它是即时的!
when I go and show the dialogue a second time, it's instant!
我的列表框到目前为止显示了1个项目.
注释:
public static class PreLoadedResources
{
public static IEnumerable<ProjectType> projectTypes;
}
推荐答案
每次添加项目时,ListBox
必须重绘.您可以使用Dmitry的AddRange()
方法,也可以使用BeginUpdate()
/EndUpdate()
调用包装循环.
The ListBox
has to redraw every time you add an item. You can either use Dmitry's method of using AddRange()
, or you can wrap your loop with BeginUpdate()
/EndUpdate()
calls.
ListBoxProjectType.BeginUpdate();
foreach( var projectType in projectTypes )
{
ListBoxProjectType.Items.Add(projectType.Title);
}
ListBoxProjectType.EndUpdate();
这篇关于与ListBox的显示/绘图对话缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!