终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

150 lines
4.3 KiB

  1. using BPASmartClient.Helper;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Bus.DataBus
  9. {
  10. /// <summary>
  11. /// 线程集中处理委托
  12. /// </summary>
  13. /// <param name="action"></param>
  14. public delegate void ActionKeyHandle(string action);
  15. public delegate void ThreadExceptionHandle(string action, Exception ex);
  16. public delegate void ThreadExitHandle(string action);
  17. /// <summary>
  18. /// 执行器
  19. /// </summary>
  20. public class Executer : Singleton<Executer>
  21. {
  22. private System.Timers.Timer _timer4Monitor = new System.Timers.Timer(1000);
  23. private ConcurrentDictionary<string, Thread> _actions = new ConcurrentDictionary<string, Thread>();
  24. private object _async = new object();
  25. public event ThreadExceptionHandle OnThreadException;
  26. public event ThreadExitHandle ThreadExit;
  27. /// <summary>
  28. /// 构造器
  29. /// </summary>
  30. public Executer()
  31. {
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public void Dispose()
  37. {
  38. _timer4Monitor.Stop();
  39. foreach (var th in _actions)
  40. {
  41. try { th.Value.Abort(); }
  42. catch (ThreadAbortException) { }
  43. }
  44. }
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. /// <returns></returns>
  49. public List<Thread> GetActionInfos()
  50. {
  51. Monitor.TryEnter(_async, 200);
  52. var actionInfos = _actions.Values.ToList();
  53. Monitor.Exit(_async);
  54. return actionInfos;
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. /// <param name="key"></param>
  60. public void Abort(string key)
  61. {
  62. Monitor.TryEnter(_async, 200);
  63. if (_actions.ContainsKey(key))
  64. {
  65. try { _actions[key].Abort(); }
  66. catch (ThreadAbortException) { }
  67. }
  68. Monitor.Exit(_async);
  69. }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="key"></param>
  74. /// <returns></returns>
  75. public bool ContainsKey(string key)
  76. {
  77. Monitor.TryEnter(_async, 200);
  78. var item = _actions[key];
  79. Monitor.Exit(_async);
  80. if (null != item)
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. /// <param name="action"></param>
  90. /// <param name="name"></param>
  91. /// <param name="isBackground"></param>
  92. /// <param name="priority"></param>
  93. public void Start(Action action, string name, bool isBackground = false, ThreadPriority priority = ThreadPriority.Normal)
  94. {
  95. Thread thread = new Thread(() =>
  96. {
  97. try
  98. {
  99. action();
  100. ThreadExit?.Invoke(name);
  101. }
  102. catch (Exception ex)
  103. {
  104. OnThreadException?.Invoke(name, ex);
  105. }
  106. });
  107. thread.IsBackground = isBackground;
  108. thread.Priority = priority;
  109. thread.Name = name;
  110. thread.Start();
  111. Monitor.TryEnter(_async, 50);
  112. if (_actions.ContainsKey(name))
  113. {
  114. try { _actions[name].Abort(); }
  115. catch (ThreadAbortException) { }
  116. }
  117. _actions[name] = thread;
  118. Monitor.Exit(_async);
  119. }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="action"></param>
  124. /// <param name="timeout"></param>
  125. /// <returns></returns>
  126. public bool StartWhiteReturn(Func<bool> action, int timeout = 3)
  127. {
  128. DateTime beginTime = DateTime.Now;
  129. bool doResult = false;
  130. while (DateTime.Now.Subtract(beginTime).TotalSeconds <= 3 && !doResult)
  131. {
  132. doResult = action();
  133. }
  134. return doResult;
  135. }
  136. /// <summary>
  137. ///
  138. /// </summary>
  139. public event ActionKeyHandle ActionAbort;
  140. /// <summary>
  141. ///
  142. /// </summary>
  143. public event ActionKeyHandle ActionStarted;
  144. }
  145. }