System.DirectoryServices.AccountManagement.GroupPrincipal 线程安全吗?
我正在编写一个将批量创建用户的程序,我有一个操作是创建的一部分,该操作会阻塞大约 5 秒,为了解决这个问题,我将使其线程化并将所有内容都放在线程池中。
我的问题是,如果我在线程外部创建原则并将组原则传递给线程并调用group.Members.Add(u)
和group.Save()
可以我遇到麻烦了吗?从每个线程内部生成一个新的组主体会更好吗?
I am writing a program that will create users in bulk, I have a operation that is part of the creation that is blocks for about 5 seconds, to get around this I was going to make it threaded and have everything sitting in a thread pool.
My question is if I create the principle outside the thread and pass the group principle to the thread and call group.Members.Add(u)
and group.Save()
can i get in trouble? would it be better to generate a new group principal from inside each thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何时候两个线程作用于同一个集合实例,你都是不安全的。如果您仅使用单个后台线程,因此 UI 不会阻塞,则在后台线程中创建该组并在那里专门使用它。如果您的计划是通过生成多个线程同时访问 Active Directory 来加速将用户添加到组中,我猜这不会有帮助。
Any time two threads act on the same collection instance, you are not safe. If you're just using a single background thread so the UI doesn't block, then create the group in the background thread and use it there exclusively. If your plan is to speed up adding users to a group by spawning multiple threads to hit Active Directory at the same time, I'm guessing that won't help.
来自 MSDN:
这个小注释在文档中很常见。由程序员决定“线程安全”的含义以及如何控制并发访问。
From MSDN:
That little note is a very common one in the documentation. It's up to you the programmer to decide what you mean by "thread safe" and how to control concurrent access.