我这里有一个桌面应用程序ListBox
,它将接受超过 10,000 条记录(目录及其子目录中的文件)。当我为其分配超过 50,000DataSource
的DataTable
值时,即使它位于DoWork
a的内部,它也会使 UI 挂起BackgroundWorker
,因此,我也挂起ProgressBar
,这表明ListBox
.
我还使用了这里的方法来避免在分配它时出现交叉线程DisplayMember
,ValueMember
但它仍然挂起。
这是代码:
private void bgWorkerForLstBox1_DoWork(object sender, DoWorkEventArgs e)
{
string viewPath = string.Empty;
if (radFullPath.Checked)
viewPath = "fullPath";
else if (radShortPath.Checked)
viewPath = "truncatedPath";
else
viewPath = "fileName";
if (dt1 != null)
if (dt1.Rows.Count > 0)
SetListBox1Props(viewPath, "fullPath");
}
delegate void SetListBox1PropsCallback(string DisplayMember, string ValueMember);
private void SetListBox1Props(string DisplayMember, string ValueMember)
{
if (this.lstBox1.InvokeRequired)
{
SetListBox1PropsCallback d = new SetListBox1PropsCallback(SetListBox1Props);
this.Invoke(d, new object[] { DisplayMember, ValueMember });
}
else
{
this.lstBox1.DataSource = dt1;
this.lstBox1.DisplayMember = DisplayMember;
this.lstBox1.ValueMember = ValueMember;
}
}