【发布时间】:2014-10-20 06:17:23
【问题描述】:
我想在我的 Windows 应用程序中实现后台工作程序。 目前我正在使用按钮事件处理程序来加载带有数据的组合框。由于查询挂起用户界面,我想实现后台工作者,因为查询在不同的线程中运行。我从来没有在我的任何应用程序中使用过这个后台工作者。我对此进行了一些研究,但仍然无法实施。任何帮助或建议将不胜感激。
这就是我的按钮事件处理程序的样子
private void button6_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "All")
{
findAllUser();
}
else
{
//Do Something!!!
}
}
findAllUser() 将从活动目录中获取所有用户,这通常需要时间并使 UI 无响应。 findAllUser() 的代码如下所示。
public void findAllUser()
{
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://DC=xyz, DC=com");
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user))";
foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
{
try
{
System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
comboBox2.Items.Add(de.Properties["GivenName"].Value.ToString() + " " + de.Properties["sn"].Value.ToString() + " " + "[" + de.Properties["sAMAccountName"].Value.ToString() + "]");
}
catch (Exception)
{
// MessageBox.Show(e.ToString());
}
}
}
下面是后台工作人员现在的样子..都是空的
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
任何建议我如何实现上面的代码,以便后台工作人员使用活动目录用户列表填充组合框2。
【问题讨论】:
-
网络上有大量的基本示例演示了 Background 的 worker 简单使用。好好看看,让你的代码工作,来吧...dotnetperls.com/backgroundworker
标签: c# winforms active-directory backgroundworker