ソフトウェアのデモに使用するターミナルサーバー上のユーザーを管理できるプログラムがあります。私はシステムにユーザーを追加するパフォーマンスを改善しようとしています(メインアカウントを追加し、必要に応じてサブアカウントを追加します。たとえば、Demo1のユーザーと3人のサブユーザーがいる場合、Demo1、Demo1a、Demo1b、およびDemo1c。)
private void AddUsers(UserInfo userInfo, InfinityInfo infinityInfo, int subUserStart)
{
using (GroupPrincipal r = GroupPrincipal.FindByIdentity(context, "Remote Desktop Users"))
using (GroupPrincipal u = GroupPrincipal.FindByIdentity(context, "Users"))
for(int i = subUserStart; i < userInfo.SubUsers; ++i)
{
string username = userInfo.Username;
if (i >= 0)
{
username += (char)('a' + i);
}
UserPrincipal user = null;
try
{
if (userInfo.NewPassword == null)
throw new ArgumentNullException("userInfo.NewPassword", "userInfo.NewPassword was null");
if (userInfo.NewPassword == "")
throw new ArgumentOutOfRangeException("userInfo.NewPassword", "userInfo.NewPassword was empty");
user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
if (user == null)
{
user = new UserPrincipal(context, username, userInfo.NewPassword, true);
user.UserCannotChangePassword = true;
user.PasswordNeverExpires = true;
user.Save();
r.Members.Add(user);
u.Members.Add(user);
}
else
{
user.Enabled = true;
user.SetPassword(userInfo.NewPassword);
}
IADsTSUserEx iad = (IADsTSUserEx)((DirectoryEntry)user.GetUnderlyingObject()).NativeObject;
iad.TerminalServicesInitialProgram = GenerateProgramString(infinityInfo);
iad.TerminalServicesWorkDirectory = Service.Properties.Settings.Default.StartInPath;
iad.ConnectClientDrivesAtLogon = 0;
user.Save();
r.Save();
u.Save();
OperationContext.Current.GetCallbackChannel<IRemoteUserManagerCallback>().FinishedChangingUser(username);
}
catch (Exception e)
{
string errorString = String.Format("Could not Add User:{0} Sub user:{1}", userInfo.Username, i);
try
{
if (user != null)
errorString += "\nSam Name: " + user.SamAccountName;
}
catch { }
OperationContext.Current.GetCallbackChannel<IRemoteUserManagerCallback>().UserException(errorString, e);
}
finally
{
if (user != null)
user.Dispose();
}
}
}
私が見つけたコードをステップスルーするuser = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
と、ループごとに5〜10秒かかる、コストのかかる呼び出しになります。
すべての呼び出しでさらに5〜10秒のヒットがGroupPrincipal.FindByIdentity()
あったので、ループの外に移動しましたSave()
。これは高価ではありません。これをスピードアップするのに役立つ他の推奨事項はありますか?
編集-通常の場合、ユーザーは存在しますが、サブユーザーは存在しない可能性がありますが、存在する可能性があります。