Queue.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Text;
  7. using System.Text.Json;
  8. namespace ConvertLog.common
  9. {
  10. public class Queue
  11. {
  12. private int maxQueueSize = 0;
  13. private ArrayList queueData = new ArrayList();
  14. private object mutex;
  15. public Queue()
  16. {
  17. maxQueueSize = 1000000;
  18. mutex = this;
  19. }
  20. public Queue(int maxSize)
  21. {
  22. maxQueueSize = maxSize;
  23. mutex = this;
  24. }
  25. public int Size()
  26. {
  27. lock (mutex)
  28. {
  29. return queueData.Count;
  30. }
  31. }
  32. public bool IsEmpty()
  33. {
  34. lock (mutex)
  35. {
  36. return (queueData.Count == 0);
  37. }
  38. }
  39. public object Dequeue()
  40. {
  41. lock (mutex)
  42. {
  43. object first = null;
  44. if (queueData.Count > 0)
  45. {
  46. first = queueData[0];
  47. queueData.RemoveAt(0);
  48. }
  49. return first;
  50. }
  51. }
  52. public object Dequeue(object obj)
  53. {
  54. object found = null;
  55. lock (mutex)
  56. {
  57. found = queueData.Contains(obj);
  58. if (found != null)
  59. {
  60. queueData.Remove(obj);
  61. }
  62. }
  63. return obj;
  64. }
  65. public void Enqueue(Object obj)
  66. {
  67. lock (mutex)
  68. {
  69. if (queueData.Count >= maxQueueSize)
  70. {
  71. throw new IndexOutOfRangeException("Queue is full. Element not added.");
  72. }
  73. queueData.Add(obj);
  74. }
  75. }
  76. public void Clear()
  77. {
  78. lock (mutex)
  79. {
  80. if (queueData.Count >= 0)
  81. {
  82. queueData.Clear();
  83. }
  84. }
  85. }
  86. public object Find(object obj)
  87. {
  88. lock (mutex)
  89. {
  90. object current;
  91. IEnumerator iter = queueData.GetEnumerator();
  92. while (iter.MoveNext())
  93. {
  94. current = iter.Current;
  95. if (current.Equals(obj))
  96. {
  97. return current;
  98. }
  99. }
  100. }
  101. return null;
  102. }
  103. public ArrayList ToArrayList()
  104. {
  105. return queueData;
  106. }
  107. public ArrayList ToArrayListAndClear()
  108. {
  109. lock (mutex)
  110. {
  111. ArrayList result = new ArrayList();
  112. if (queueData.Count >= 0)
  113. {
  114. result.AddRange(queueData);
  115. queueData.Clear();
  116. }
  117. return result;
  118. }
  119. }
  120. public void SaveQueue(string file_name)
  121. {
  122. // Chỉ lưu những phần tử đúng kiểu oneImportObj (đang được sử dụng trong project)
  123. var typedList = new List<oneImportObj>();
  124. foreach (var item in queueData)
  125. {
  126. if (item is oneImportObj obj)
  127. {
  128. typedList.Add(obj);
  129. }
  130. }
  131. var options = new JsonSerializerOptions
  132. {
  133. WriteIndented = false
  134. };
  135. string json = JsonSerializer.Serialize(typedList, options);
  136. File.WriteAllText(file_name, json);
  137. }
  138. public int LoadQueue(string file_name)
  139. {
  140. if (File.Exists(file_name))
  141. {
  142. string json = File.ReadAllText(file_name);
  143. if (!string.IsNullOrWhiteSpace(json))
  144. {
  145. var list = JsonSerializer.Deserialize<List<oneImportObj>>(json) ?? new List<oneImportObj>();
  146. lock (mutex)
  147. {
  148. queueData.Clear();
  149. foreach (var item in list)
  150. {
  151. queueData.Add(item);
  152. }
  153. }
  154. }
  155. }
  156. return queueData.Count;
  157. }
  158. //public void SaveQueue(string file_name)
  159. //{
  160. // if (File.Exists(file_name))
  161. // {
  162. // File.Delete(file_name);
  163. // }
  164. // Stream st = File.Create(file_name);
  165. // BinaryFormatter bf = new BinaryFormatter();
  166. // bf.Serialize(st, queueData);
  167. // st.Close();
  168. //}
  169. //public int LoadQueue(string file_name)
  170. //{
  171. // if (File.Exists(file_name))
  172. // {
  173. // Stream st = File.OpenRead(file_name);
  174. // if (st.Length > 0)
  175. // {
  176. // BinaryFormatter bf = new BinaryFormatter();
  177. // queueData = (ArrayList)bf.Deserialize(st);
  178. // }
  179. // st.Close();
  180. // //File.Delete(file_name);
  181. // }
  182. // return queueData.Count;
  183. //}
  184. }
  185. }