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

149 line
5.0 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. if (!actions.ContainsKey(key))
  89. {
  90. if (action is Action actionBus)
  91. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  92. if (action is Action<object> actionObj)
  93. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  94. if (action is Action<object[]> actionObjs)
  95. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  96. if (action is Func<object> funcObj)
  97. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  98. if (action is Func<object, object> puncPar)
  99. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  100. }
  101. }
  102. }
  103. }
  104. }
  105. internal class Delegation
  106. {
  107. /// <summary>
  108. /// 带参数的委托
  109. /// </summary>
  110. public Action<object> ActionPar { get; set; }
  111. /// <summary>
  112. /// 带参数的委托
  113. /// </summary>
  114. public Action<object[]> ActionPars { get; set; }
  115. /// <summary>
  116. /// 无参数的委托
  117. /// </summary>
  118. public Action ActionBus { get; set; }
  119. /// <summary>
  120. /// 有返回值的委托
  121. /// </summary>
  122. public Func<object> FuncObj { get; set; }
  123. /// <summary>
  124. /// 有返回值,有参数的委托
  125. /// </summary>
  126. public Func<object, object> FuncPar { get; set; }
  127. }
  128. }