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

157 lines
5.3 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. namespace BPASmartClient.Helper
  8. {
  9. public class ActionManage
  10. {
  11. private volatile static ActionManage _Instance;
  12. public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
  13. private ActionManage() { }
  14. //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
  15. private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  16. static readonly object SendLock = new object();
  17. static readonly object SendParLock = new object();
  18. static readonly object RegisterLock = new object();
  19. /// <summary>
  20. /// 注销委托
  21. /// </summary>
  22. /// <param name="key"></param>
  23. public void CancelRegister(string key)
  24. {
  25. if (actions.ContainsKey(key))
  26. actions.TryRemove(key, out Delegation t);
  27. }
  28. /// <summary>
  29. /// 执行注册过的委托
  30. /// </summary>
  31. /// <param name="key">注册委托的key</param>
  32. /// <param name="par">委托参数</param>
  33. /// <param name="Callback">委托回调</param>
  34. public void Send(string key, object par, Action Callback = null)
  35. {
  36. lock (SendLock)
  37. if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
  38. }
  39. /// <summary>
  40. /// 执行注册过的委托
  41. /// </summary>
  42. /// <param name="key">注册委托的key</param>
  43. /// <param name="par">委托参数</param>
  44. /// <param name="Callback">委托回调</param>
  45. public void Send(string key, object[] par, Action Callback = null)
  46. {
  47. lock (SendLock)
  48. if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
  49. }
  50. /// <summary>
  51. /// 执行注册过的委托
  52. /// </summary>
  53. /// <param name="key">注册委托的key</param>
  54. /// <param name="Callback">委托回调</param>
  55. public void Send(string key, Action Callback = null)
  56. {
  57. lock (SendLock)
  58. if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
  59. }
  60. public object SendResult(string key, object par = null)
  61. {
  62. lock (SendLock)
  63. if (actions.ContainsKey(key))
  64. if (par == null)
  65. {
  66. return actions[key].FuncObj?.Invoke();
  67. }
  68. else
  69. {
  70. return actions[key].FuncPar?.Invoke(par);
  71. }
  72. return default;
  73. }
  74. /// <summary>
  75. /// 根据 Key 注册需要执行的委托
  76. /// </summary>
  77. /// <typeparam name="T"></typeparam>
  78. /// <param name="action">委托方法</param>
  79. /// <param name="key">委托key</param>
  80. /// <param name="IsAutoCancelRegister">是否自动取消注册</param>
  81. public void Register<T>(T action, string key, bool IsAutoCancelRegister = false)
  82. {
  83. lock (RegisterLock)
  84. {
  85. if (action != null)
  86. {
  87. if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
  88. try
  89. {
  90. if (!actions.ContainsKey(key))
  91. {
  92. if (action is Action actionBus)
  93. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  94. if (action is Action<object> actionObj)
  95. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  96. if (action is Action<object[]> actionObjs)
  97. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  98. if (action is Func<object> funcObj)
  99. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  100. if (action is Func<object, object> puncPar)
  101. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString());
  107. }
  108. }
  109. }
  110. }
  111. }
  112. internal class Delegation
  113. {
  114. /// <summary>
  115. /// 带参数的委托
  116. /// </summary>
  117. public Action<object> ActionPar { get; set; }
  118. /// <summary>
  119. /// 带参数的委托
  120. /// </summary>
  121. public Action<object[]> ActionPars { get; set; }
  122. /// <summary>
  123. /// 无参数的委托
  124. /// </summary>
  125. public Action ActionBus { get; set; }
  126. /// <summary>
  127. /// 有返回值的委托
  128. /// </summary>
  129. public Func<object> FuncObj { get; set; }
  130. /// <summary>
  131. /// 有返回值,有参数的委托
  132. /// </summary>
  133. public Func<object, object> FuncPar { get; set; }
  134. }
  135. }