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 trou开发者_开发知识库ble? would it be better to generate a new group principal from inside each thread?
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.
From MSDN:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
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.
精彩评论