我的应用程序使用实体框架来提取一小部分结果......它需要大约 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 缓慢显示/绘制对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!