You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListLocked.cs 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Preparation.Utility
  6. {
  7. public class LockedClassList<T> : IEnumerable
  8. where T : class
  9. {
  10. private readonly ReaderWriterLockSlim listLock = new();
  11. private List<T> list;
  12. #region 构造
  13. public LockedClassList()
  14. {
  15. list = new List<T>();
  16. }
  17. public LockedClassList(int capacity)
  18. {
  19. list = new List<T>(capacity);
  20. }
  21. public LockedClassList(IEnumerable<T> collection)
  22. {
  23. list = new List<T>(collection);
  24. }
  25. #endregion
  26. #region 修改
  27. public TResult WriteLock<TResult>(Func<TResult> func)
  28. {
  29. listLock.EnterWriteLock();
  30. try
  31. {
  32. return func();
  33. }
  34. finally
  35. {
  36. listLock.ExitWriteLock();
  37. }
  38. }
  39. public void WriteLock(Action func)
  40. {
  41. listLock.EnterWriteLock();
  42. try
  43. {
  44. func();
  45. }
  46. finally
  47. {
  48. listLock.ExitWriteLock();
  49. }
  50. }
  51. public void Add(T item)
  52. {
  53. WriteLock(() => { list.Add(item); });
  54. }
  55. public void Insert(int index, T item)
  56. {
  57. WriteLock(() => { list.Insert(index, item); });
  58. }
  59. public void Clear()
  60. {
  61. WriteLock(() => { list.Clear(); });
  62. }
  63. public bool Remove(T item)
  64. {
  65. return WriteLock<bool>(() => { return list.Remove(item); });
  66. }
  67. public int RemoveAll(T item) => WriteLock(() => { return list.RemoveAll((t) => { return t == item; }); });
  68. public bool RemoveOne(Predicate<T> match) =>
  69. WriteLock(() =>
  70. {
  71. int index = list.FindIndex(match);
  72. if (index == -1) return false;
  73. list.RemoveAt(index);
  74. return true;
  75. });
  76. public int RemoveAll(Predicate<T> match) => WriteLock(() => { return list.RemoveAll(match); });
  77. public bool RemoveAt(int index)
  78. {
  79. return WriteLock(() =>
  80. {
  81. if (index > list.Count) return false;
  82. list.RemoveAt(index);
  83. return true;
  84. });
  85. }
  86. #endregion
  87. #region 读取与对类操作
  88. public TResult ReadLock<TResult>(Func<TResult> func)
  89. {
  90. listLock.EnterReadLock();
  91. try
  92. {
  93. return func();
  94. }
  95. finally
  96. {
  97. listLock.ExitReadLock();
  98. }
  99. }
  100. public void ReadLock(Action func)
  101. {
  102. listLock.EnterReadLock();
  103. try
  104. {
  105. func();
  106. }
  107. finally
  108. {
  109. listLock.ExitReadLock();
  110. }
  111. }
  112. public T this[int index]
  113. {
  114. get
  115. {
  116. return ReadLock<T>(() => { return list[index]; });
  117. }
  118. set
  119. {
  120. ReadLock(() => { list[index] = value!; });
  121. }
  122. }
  123. public int Count => ReadLock(() => { return list.Count; });
  124. public int IndexOf(T item)
  125. {
  126. return ReadLock(() => { return list.IndexOf(item); });
  127. }
  128. public Array ToArray()
  129. {
  130. return ReadLock(() => { return list.ToArray(); });
  131. }
  132. public List<T> ToNewList()
  133. {
  134. List<T> lt = new();
  135. return ReadLock(() => { lt.AddRange(list); return lt; });
  136. }
  137. public bool Contains(T item)
  138. {
  139. return ReadLock(() => { return list.Contains(item); });
  140. }
  141. public T? Find(Predicate<T> match)
  142. {
  143. return ReadLock(() => { return list.Find(match); });
  144. }
  145. public List<T> FindAll(Predicate<T> match)
  146. {
  147. return ReadLock(() => { return list.FindAll(match); });
  148. }
  149. public int FindIndex(Predicate<T> match) => ReadLock(() => { return list.FindIndex(match); });
  150. public void ForEach(Action<T> action) => ReadLock(() => { list.ForEach(action); });
  151. public IEnumerator GetEnumerator()
  152. {
  153. return ReadLock(() => { return list.GetEnumerator(); });
  154. }
  155. #endregion
  156. }
  157. }