MainThread.cs 6.1 KB

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