MainThread.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Threading;
  6. namespace ConvertLog.thread
  7. {
  8. public abstract class MainThread
  9. {
  10. //Cac thuoc tinh cua Thead
  11. private const String PROCESSING_THREAD_NAME = "ProcThread";
  12. private const byte PROC_INITIALISING = 0;
  13. private const byte PROC_RECEIVING = 1;
  14. private const byte PROC_FINISHED = 2;
  15. private object _ProcessingStatusLock = new object();
  16. private byte _ProcessingStatus = PROC_INITIALISING;
  17. private int _ThreadIndex = 0;
  18. private bool _KeepProcessing = true;
  19. private Exception _TermException = null;
  20. private Thread _ProcessingThread = null;
  21. private bool _ThreadSleeping = false;
  22. public abstract void Process();
  23. //Cac phuong thuc cua thread
  24. public virtual void Start(string _name)
  25. {
  26. if (!IsProcessing())
  27. {
  28. // i.e. is initialising or finished
  29. SetProcessingStatus(PROC_INITIALISING);
  30. _TermException = null;
  31. _KeepProcessing = true;
  32. //To name a delegate.
  33. ThreadStart ts = new ThreadStart(this.Run);
  34. _ProcessingThread = new Thread(ts);
  35. _ProcessingThread.Name = _name;
  36. _ProcessingThread.Start();
  37. while (IsInitialising())
  38. {
  39. Sleep(30);
  40. }
  41. }
  42. }//End of Start by name
  43. public virtual int GetIndex()
  44. {
  45. return _ThreadIndex;
  46. }
  47. public virtual void Start(int _index)
  48. {
  49. if (!IsProcessing())
  50. {
  51. // i.e. is initialising or finished
  52. SetProcessingStatus(PROC_INITIALISING);
  53. _TermException = null;
  54. _KeepProcessing = true;
  55. //To name a delegate.
  56. ThreadStart ts = new ThreadStart(this.Run);
  57. _ProcessingThread = new Thread(ts);
  58. _ThreadIndex = _index;
  59. _ProcessingThread.Start();
  60. while (IsInitialising())
  61. {
  62. Sleep(30);
  63. }
  64. }
  65. }//End of Start by name
  66. public virtual void Start()
  67. {
  68. if (!IsProcessing())
  69. {
  70. SetProcessingStatus(PROC_INITIALISING);
  71. _TermException = null;
  72. _KeepProcessing = true;
  73. ThreadStart ts = new ThreadStart(this.Run);
  74. _ProcessingThread = new Thread(ts);
  75. _ProcessingThread.Name = GenerateDefaultIndexedThreadName();
  76. _ProcessingThread.Start();
  77. while (IsInitialising())
  78. {
  79. Sleep(10);
  80. }
  81. }
  82. }//End of Start
  83. public virtual void Stop(int timeout)
  84. {
  85. if (IsProcessing())
  86. {
  87. StopProcessing(null);
  88. if (_ProcessingThread.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
  89. {
  90. _ProcessingThread.Interrupt();
  91. }
  92. DateTime dt = DateTime.Now;
  93. while (!IsFinished())
  94. {
  95. TimeSpan tp = DateTime.Now - dt;
  96. if (tp.TotalMilliseconds < timeout)
  97. {
  98. Sleep(10);
  99. }
  100. else
  101. {
  102. _ProcessingThread.Abort();
  103. }
  104. }
  105. }
  106. }
  107. public virtual void Stop()
  108. {
  109. if (IsProcessing())
  110. {
  111. StopProcessing(null);
  112. if (_ProcessingThread.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
  113. {
  114. _ProcessingThread.Interrupt();
  115. }
  116. while (!IsFinished())
  117. {
  118. Sleep(10);
  119. }
  120. }
  121. }
  122. protected void StopProcessing(Exception e)
  123. {
  124. SetTermException(e);
  125. _KeepProcessing = false;
  126. }
  127. public void Run()
  128. {
  129. try
  130. {
  131. SetProcessingStatus(PROC_RECEIVING);
  132. while (_KeepProcessing)
  133. {
  134. try
  135. {
  136. Process();
  137. }
  138. catch (Exception e1)
  139. {
  140. System.Console.WriteLine(e1.ToString());
  141. _KeepProcessing = false;
  142. }
  143. Sleep(10);
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. SetTermException(e);
  149. }
  150. finally
  151. {
  152. SetProcessingStatus(PROC_FINISHED);
  153. //debug_.exit(DUTL,this);
  154. }
  155. }
  156. public String GenerateDefaultIndexedThreadName()
  157. {
  158. return GetDefaultThreadName() + "-" + GetThreadIndex();
  159. }
  160. public virtual String GetThreadName()
  161. {
  162. return _ProcessingThread.Name;
  163. }
  164. public String GetDefaultThreadName()
  165. {
  166. return PROCESSING_THREAD_NAME;
  167. }
  168. public int GetThreadIndex()
  169. {
  170. return ++_ThreadIndex;
  171. }
  172. public int GetSystemCurrentThreadID()
  173. {
  174. return AppDomain.GetCurrentThreadId();
  175. }
  176. protected void SetTermException(Exception e) { _TermException = e; }
  177. public Exception GetTermException()
  178. {
  179. return _TermException;
  180. }
  181. private void SetProcessingStatus(byte value)
  182. {
  183. lock (_ProcessingStatusLock)
  184. {
  185. _ProcessingStatus = value;
  186. }
  187. }
  188. private bool IsInitialising()
  189. {
  190. lock (_ProcessingStatusLock)
  191. {
  192. return _ProcessingStatus == PROC_INITIALISING;
  193. }
  194. }
  195. public bool IsProcessing()
  196. {
  197. lock (_ProcessingStatusLock)
  198. {
  199. return _ProcessingStatus == PROC_RECEIVING;
  200. }
  201. }
  202. public bool IsFinished()
  203. {
  204. lock (_ProcessingStatusLock)
  205. {
  206. return _ProcessingStatus == PROC_FINISHED;
  207. }
  208. }
  209. public void Sleep(int timeout)
  210. {
  211. try
  212. {
  213. _ThreadSleeping = true;
  214. Thread.Sleep(timeout);//wait 1 minute
  215. _ThreadSleeping = false;
  216. }
  217. catch (ThreadInterruptedException e)
  218. {
  219. _ThreadSleeping = false;
  220. //Console.WriteLine(e.Message);
  221. }
  222. }
  223. public bool IsSleeping()
  224. {
  225. return _ThreadSleeping;
  226. }
  227. }
  228. }