终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ActionManage.cs 10 KiB

il y a 2 ans
il y a 1 an
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. namespace BPASmartClient.Helper
  11. {
  12. public class ActionManage
  13. {
  14. private volatile static ActionManage _Instance;
  15. public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
  16. private ActionManage() { }
  17. //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
  18. private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  19. static readonly object SendLock = new object();
  20. static readonly object SendParLock = new object();
  21. static readonly object RegisterLock = new object();
  22. /// <summary>
  23. /// 注销委托
  24. /// </summary>
  25. /// <param name="key"></param>
  26. public void CancelRegister(string key)
  27. {
  28. if (actions.ContainsKey(key))
  29. actions.TryRemove(key, out Delegation t);
  30. }
  31. /// <summary>
  32. /// 执行注册过的委托
  33. /// </summary>
  34. /// <param name="key">注册委托的key</param>
  35. /// <param name="par">委托参数</param>
  36. /// <param name="Callback">委托回调</param>
  37. public void Send(string key, object par, Action Callback = null)
  38. {
  39. lock (SendLock)
  40. if (actions.ContainsKey(key))
  41. 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. }