终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

141 Zeilen
4.6 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. public void Register<T>(T action, string key)
  75. {
  76. lock (RegisterLock)
  77. {
  78. if (action != null)
  79. {
  80. if (!actions.ContainsKey(key))
  81. {
  82. if (action is Action actionBus)
  83. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  84. if (action is Action<object> actionObj)
  85. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  86. if (action is Action<object[]> actionObjs)
  87. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  88. if (action is Func<object> funcObj)
  89. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  90. if (action is Func<object, object> puncPar)
  91. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  92. }
  93. }
  94. }
  95. }
  96. }
  97. internal class Delegation
  98. {
  99. /// <summary>
  100. /// 带参数的委托
  101. /// </summary>
  102. public Action<object> ActionPar { get; set; }
  103. /// <summary>
  104. /// 带参数的委托
  105. /// </summary>
  106. public Action<object[]> ActionPars { get; set; }
  107. /// <summary>
  108. /// 无参数的委托
  109. /// </summary>
  110. public Action ActionBus { get; set; }
  111. /// <summary>
  112. /// 有返回值的委托
  113. /// </summary>
  114. public Func<object> FuncObj { get; set; }
  115. /// <summary>
  116. /// 有返回值,有参数的委托
  117. /// </summary>
  118. public Func<object, object> FuncPar { get; set; }
  119. }
  120. }