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

129 lines
4.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, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  15. static readonly object SendLock = new object();
  16. static readonly object SendParLock = new object();
  17. static readonly object RegisterLock = new object();
  18. /// <summary>
  19. /// 注销委托
  20. /// </summary>
  21. /// <param name="key"></param>
  22. public void CancelRegister(string key)
  23. {
  24. if (actions.ContainsKey(key))
  25. actions.TryRemove(key, out Delegation t);
  26. }
  27. /// <summary>
  28. /// 执行注册过的委托
  29. /// </summary>
  30. /// <param name="key">注册委托的key</param>
  31. /// <param name="par">委托参数</param>
  32. /// <param name="Callback">委托回调</param>
  33. public void Send(string key, object par, Action Callback = null)
  34. {
  35. lock (SendLock)
  36. if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
  37. //if (actions[key].ActionPar != null)
  38. //{
  39. // actions[key].ActionPar(par);
  40. // if (Callback != null) Callback();
  41. //}
  42. }
  43. /// <summary>
  44. /// 执行注册过的委托
  45. /// </summary>
  46. /// <param name="key">注册委托的key</param>
  47. /// <param name="Callback">委托回调</param>
  48. public void Send(string key, Action Callback = null)
  49. {
  50. lock (SendLock)
  51. if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
  52. }
  53. public object SendResult(string key, object par = null)
  54. {
  55. lock (SendLock)
  56. if (actions.ContainsKey(key))
  57. if (par == null)
  58. {
  59. if (actions[key].FuncObj != null)
  60. return actions[key].FuncObj;
  61. }
  62. else
  63. {
  64. if (actions[key].FuncPar != null)
  65. return actions[key].FuncPar(par);
  66. }
  67. return default;
  68. }
  69. public void Register<T>(T action, string key)
  70. {
  71. lock (RegisterLock)
  72. {
  73. if (action != null)
  74. {
  75. if (!actions.ContainsKey(key))
  76. {
  77. if (action is Action actionBus)
  78. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  79. if (action is Action<object> actionObj)
  80. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  81. if (action is Func<object> funcObj)
  82. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  83. if (action is Func<object, object> puncPar)
  84. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  85. }
  86. }
  87. }
  88. }
  89. }
  90. internal class Delegation
  91. {
  92. /// <summary>
  93. /// 带参数的委托
  94. /// </summary>
  95. public Action<object> ActionPar { get; set; }
  96. /// <summary>
  97. /// 无参数的委托
  98. /// </summary>
  99. public Action ActionBus { get; set; }
  100. public Action CallBack { get; set; }
  101. /// <summary>
  102. /// 有返回值的委托
  103. /// </summary>
  104. public Func<object> FuncObj { get; set; }
  105. /// <summary>
  106. /// 有返回值,有参数的委托
  107. /// </summary>
  108. public Func<object, object> FuncPar { get; set; }
  109. }
  110. }