public T this[int index] { get { using (_lock.Read()) return(_list[index]); } set { using (_lock.Write()) _list[index] = value; } }
//获取第一个元素并从集合中移除 public string LootFirst() { Console.WriteLine("当前线程:" + Thread.CurrentThread.ManagedThreadId); using (_Lock.Write()) { if (_List.Count() == 0) { _Lock.Enabled = false; return(null); } var s = _List[0]; _List.RemoveAt(0); Console.WriteLine("进入当前写锁的线程:" + Thread.CurrentThread.ManagedThreadId); return(s); } }
/// <summary> /// 根据索引获取项。 /// </summary> /// <param name="index">索引。</param> /// <returns></returns> public new T this[int index] { get { using (_syncLock.Read()) { var t = base[index]; return(t); } } set { using (_syncLock.Write()) { base[index] = value; } } }