Threadsafe foreach enumeration of lists

后端 未结 11 1602

I need to enumerate though generic IList<> of objects. The contents of the list may change, as in being added or removed by other threads, and this will kill my enumerati

11条回答
  •  眼角桃花
    2020-12-17 19:24

    ICollection MyCollection;
    // Instantiate and populate the collection
    lock(MyCollection.SyncRoot) {
      // Some operation on the collection, which is now thread safe.
    }
    

    From MSDN

提交回复
热议问题