多线程调用委托方法 后台执行
此为委托:
private delegate void Info(string thisgroup,ListBox lb);
Info a =new Info(ComputerInfo);
/// <summary>
/// 获取选中工作组下的所有计算机
/// </summary>
/// <param name="thisgroup">工作组名称</param>
/// <param name="lb">ListBox2</param>
private static void ComputerInfo(string thisgroup,ListBox lb)
{
DirectoryEntry MainGroup = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry domain in MainGroup.Children)
{
if (domain.Name == thisgroup)
{
lb.Items.Clear();
foreach (DirectoryEntry pc in domain.Children)
{
if (pc.Name != "Schema")//Schema是结束标记
lb.Items.Add(pc.Name);
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < WorkGroups.Count; i++)
{
comBWG.Items.Add(WorkGroups[i]);
if (WorkGroups[i].ToString() == thisGroup)
{
comBWG.SelectedItem = thisGroup;
}
}
Thread threadComputer = new Thread(new ThreadStart(a));//错误:“Info”的重载均与委托“System.Threading.ThreadStart”不匹配
threadComputer.IsBackground = true;
threadComputer.Start();
//ComputerInfo(comBWG.SelectedItem.ToString(),listBox2);//不用多线程,此方法有效。但是因为在工作组下查找所有计算机要消耗较多时间,导致程序呈现卡死现象,所以想要用多线程的方法实现
}
lb.Items.Add(pc.Name);//这个也做对应的修改.
}
}