终端一体化运控平台
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.
 
 
 

284 lines
10 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.Concurrent;
  7. using System.Reflection.Metadata;
  8. using System.Threading;
  9. using System.Reflection;
  10. using BPASmartClient.Message;
  11. namespace BPASmartClient.Helper
  12. {
  13. public class ActionManage
  14. {
  15. private volatile static ActionManage _Instance;
  16. public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
  17. private ActionManage() { }
  18. //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
  19. private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  20. static readonly object SendLock = new object();
  21. static readonly object SendParLock = new object();
  22. static readonly object RegisterLock = new object();
  23. /// <summary>
  24. /// 注销委托
  25. /// </summary>
  26. /// <param name="key"></param>
  27. public void CancelRegister(string key)
  28. {
  29. if (actions.ContainsKey(key))
  30. actions.TryRemove(key, out Delegation t);
  31. }
  32. /// <summary>
  33. /// 执行注册过的委托
  34. /// </summary>
  35. /// <param name="key">注册委托的key</param>
  36. /// <param name="par">委托参数</param>
  37. /// <param name="Callback">委托回调</param>
  38. public void Send(string key, object par, Action Callback = null)
  39. {
  40. lock (SendLock)
  41. if (actions.ContainsKey(key))
  42. actions[key].ActionPar.Invoke(par, Callback);
  43. }
  44. public async void SendAsync(string key, object par, Action Callback = null)
  45. {
  46. await Task.Run(new Action(() =>
  47. {
  48. if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
  49. }));
  50. }
  51. /// <summary>
  52. /// 执行注册过的委托
  53. /// </summary>
  54. /// <param name="key">注册委托的key</param>
  55. /// <param name="par">委托参数</param>
  56. /// <param name="Callback">委托回调</param>
  57. public void Send(string key, object[] par, Action Callback = null)
  58. {
  59. lock (SendLock)
  60. if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
  61. }
  62. public async void SendAsync(string key, object[] par, Action Callback = null)
  63. {
  64. await Task.Run(new Action(() =>
  65. {
  66. if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
  67. }));
  68. }
  69. /// <summary>
  70. /// 执行注册过的委托
  71. /// </summary>
  72. /// <param name="key">注册委托的key</param>
  73. /// <param name="Callback">委托回调</param>
  74. public void Send(string key, Action Callback = null)
  75. {
  76. lock (SendLock)
  77. if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
  78. }
  79. public async void SendAsync(string key, Action Callback = null)
  80. {
  81. await Task.Run(new Action(() =>
  82. {
  83. if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
  84. }));
  85. }
  86. public object SendResult(string key, object par = null)
  87. {
  88. lock (SendLock)
  89. if (actions.ContainsKey(key))
  90. if (par == null)
  91. {
  92. return actions[key].FuncObj?.Invoke();
  93. }
  94. else
  95. {
  96. return actions[key].FuncPar?.Invoke(par);
  97. }
  98. return default;
  99. }
  100. public async Task<object> SendResultAsync(string key, object par = null)
  101. {
  102. return await Task.Run(new Func<object>(() =>
  103. {
  104. if (actions.ContainsKey(key))
  105. if (par == null)
  106. {
  107. return actions[key].FuncObj?.Invoke();
  108. }
  109. else
  110. {
  111. return actions[key].FuncPar?.Invoke(par);
  112. }
  113. return default;
  114. }));
  115. }
  116. /// <summary>
  117. /// 根据 Key 注册需要执行的委托
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <param name="action">委托方法</param>
  121. /// <param name="key">委托key</param>
  122. /// <param name="IsAutoCancelRegister">是否自动取消注册</param>
  123. public void Register<T>(T action, string key, bool IsAutoCancelRegister = false)
  124. {
  125. lock (RegisterLock)
  126. {
  127. if (action != null)
  128. {
  129. if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
  130. try
  131. {
  132. if (!actions.ContainsKey(key))
  133. {
  134. if (action is Action actionBus)
  135. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  136. if (action is Action<object> actionObj)
  137. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  138. if (action is Action<object[]> actionObjs)
  139. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  140. if (action is Func<object> funcObj)
  141. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  142. if (action is Func<object, object> puncPar)
  143. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString());
  149. }
  150. }
  151. }
  152. }
  153. public async void RegisterAsync<T>(T action, string key, bool IsAutoCancelRegister = false)
  154. {
  155. await Task.Run(new Action(() =>
  156. {
  157. if (action != null)
  158. {
  159. if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
  160. try
  161. {
  162. if (!actions.ContainsKey(key))
  163. {
  164. if (action is Action actionBus)
  165. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  166. if (action is Action<object> actionObj)
  167. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  168. if (action is Action<object[]> actionObjs)
  169. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  170. if (action is Func<object> funcObj)
  171. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  172. if (action is Func<object, object> puncPar)
  173. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. MessageLog.GetInstance.ShowEx(ex.ToString());
  179. }
  180. }
  181. }));
  182. }
  183. #region 升级版本
  184. //private static ConcurrentDictionary<string, meth> MethodS = new ConcurrentDictionary<string, MethodBase>();
  185. //public void Register1<TPar>(Action<TPar> action, string key, bool IsAutoCancelRegister = false)
  186. //{
  187. // MethodS.TryAdd("aa",new Action(() => { }));
  188. // lock (RegisterLock)
  189. // {
  190. // if (action != null)
  191. // {
  192. // if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
  193. // try
  194. // {
  195. // if (!actions.ContainsKey(key))
  196. // {
  197. // if (action is Action actionBus)
  198. // actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  199. // if (action is Action<TPar> actionObj)
  200. // actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  201. // if (action is Action<object[]> actionObjs)
  202. // actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  203. // if (action is Func<object> funcObj)
  204. // actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  205. // if (action is Func<object, object> puncPar)
  206. // actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  207. // }
  208. // }
  209. // catch (Exception ex)
  210. // {
  211. // Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString());
  212. // }
  213. // }
  214. // }
  215. //}
  216. #endregion
  217. }
  218. internal class Delegation
  219. {
  220. /// <summary>
  221. /// 带参数的委托
  222. /// </summary>
  223. public Action<object> ActionPar { get; set; }
  224. /// <summary>
  225. /// 带参数的委托
  226. /// </summary>
  227. public Action<object[]> ActionPars { get; set; }
  228. /// <summary>
  229. /// 无参数的委托
  230. /// </summary>
  231. public Action ActionBus { get; set; }
  232. /// <summary>
  233. /// 有返回值的委托
  234. /// </summary>
  235. public Func<object> FuncObj { get; set; }
  236. /// <summary>
  237. /// 有返回值,有参数的委托
  238. /// </summary>
  239. public Func<object, object> FuncPar { get; set; }
  240. }
  241. }