【发布时间】:2015-06-26 20:34:17
【问题描述】:
是否可以借助多线程(Task.Run 等)或任何其他好的技术来提高以下函数的响应时间?
UserCollection = new ObservableCollection<User>();
public void FillUserList(string machineName, string groupName)
{
UserCollection.Clear();
if (string.IsNullOrEmpty(machineName) || string.IsNullOrEmpty(groupName))
return;
var machineContext = new PrincipalContext(ContextType.Machine, machineName, null, ContextOptions.Negotiate);
var group = GroupPrincipal.FindByIdentity(machineContext, groupName);
var members = group.GetMembers();
foreach (var member in members)
{
var user = new User { DisplayName = member.Name, UserId = member.SamAccountName };
UserCollection.Add(user);
}
}
【问题讨论】:
-
你看到慢了吗?对于这种方法的性能,您目前的经验是什么?
-
是的,随着组内用户数的增加,性能越来越差。如果任何组有 2 个用户(5 秒)7 个用户(12-14 秒)回复!
-
代码看起来很简单我唯一能想到的就是网络问题是延迟问题,或者在你分配成员变量后尝试设置 machineContext = null 也在你的方法中放置
UserCollection对象除非您没有显示该对象的全部范围,否则我无法知道 FillUserList 被调用的频率...... -
基本上,我创建了具有两个文本框的 WPF 应用程序。用户提供机器名和组名。输入组名后,在扩展器的帮助下,我在每个组下方显示与相应组关联的用户(两个标签)。这个函数用的很频繁。
标签: c# wpf multithreading performance active-directory