终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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