code_junkie

Threadsafe foreach enumeration of lists

Nov 10th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.24 KB | None | 0 0
  1. lock(collection){
  2. foreach (object o in collection){
  3. ...
  4. }
  5. }
  6.  
  7. ICollection MyCollection;
  8. // Instantiate and populate the collection
  9. lock(MyCollection.SyncRoot) {
  10. // Some operation on the collection, which is now thread safe.
  11. }
Add Comment
Please, Sign In to add comment