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.
 
 

171 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 HBLConsole.Service
  8. {
  9. public class ActionManagerment
  10. {
  11. private volatile static ActionManagerment _Instance;
  12. public static ActionManagerment GetInstance => _Instance ?? (_Instance = new ActionManagerment());
  13. private ActionManagerment() { }
  14. private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  15. private static ConcurrentDictionary<string, ActionManagerment> actionManagers = new ConcurrentDictionary<string, ActionManagerment>();
  16. static readonly object SendLock = new object();
  17. static readonly object SendParLock = new object();
  18. static readonly object RegisterLock = new object();
  19. public void Send(string name, object token, AsyncCallback action = null)
  20. {
  21. lock (SendParLock)
  22. if (actions.ContainsKey(name))
  23. if (actions[name].ActionObj != null)
  24. {
  25. if (action != null)
  26. actions[name].ActionObj.BeginInvoke(token, action, null);
  27. else
  28. actions[name].ActionObj(token);
  29. }
  30. }
  31. public void Send(string name, AsyncCallback action = null)
  32. {
  33. lock (SendLock)
  34. if (actions.ContainsKey(name))
  35. if (actions[name].ActionBus != null)
  36. {
  37. if (action != null)
  38. actions[name].ActionBus.BeginInvoke(action, null);
  39. else
  40. actions[name].ActionBus();
  41. }
  42. }
  43. public object SendResult(string name, object token = null)
  44. {
  45. object result = new object();
  46. lock (SendParLock)
  47. if (actions.ContainsKey(name))
  48. if (token == null)
  49. {
  50. if (actions[name].FuncObj != null)
  51. result = actions[name].FuncObj;
  52. }
  53. else
  54. {
  55. if (actions[name].FuncPar != null)
  56. result = actions[name].FuncPar(token);
  57. }
  58. return result;
  59. }
  60. public void Register<T>(T action, string name)
  61. {
  62. lock (RegisterLock)
  63. {
  64. if (action != null)
  65. {
  66. if (!actions.ContainsKey(name))
  67. {
  68. if (action is Action actionBus)
  69. actions.TryAdd(name, new Delegation() { ActionBus = actionBus });
  70. if (action is Action<object> actionObj)
  71. actions.TryAdd(name, new Delegation() { ActionObj = actionObj });
  72. if (action is Func<object> funcObj)
  73. actions.TryAdd(name, new Delegation() { FuncObj = funcObj });
  74. if (action is Func<object, object> puncPar)
  75. actions.TryAdd(name, new Delegation() { FuncPar = puncPar });
  76. }
  77. }
  78. }
  79. }
  80. //public void Register(Action action, string name)
  81. //{
  82. // lock (RegisterLock)
  83. // {
  84. // if (action != null)
  85. // {
  86. // if (!actions.ContainsKey(name))
  87. // {
  88. // actions.TryAdd(name, new Delegation() { ActionBus = action });
  89. // }
  90. // }
  91. // }
  92. //}
  93. //public void Register(Action<object> action, string name)
  94. //{
  95. // lock (RegisterLock)
  96. // {
  97. // if (action != null)
  98. // {
  99. // if (!actions.ContainsKey(name))
  100. // {
  101. // actions.TryAdd(name, new Delegation() { ActionObj = action });
  102. // }
  103. // }
  104. // }
  105. //}
  106. //public void Register(Func<object> action, string name)
  107. //{
  108. // lock (RegisterLock)
  109. // {
  110. // if (action != null)
  111. // {
  112. // if (!actions.ContainsKey(name))
  113. // {
  114. // actions.TryAdd(name, new Delegation() { FuncObj = action });
  115. // }
  116. // }
  117. // }
  118. //}
  119. //public void Register(Func<object, object> action, string name)
  120. //{
  121. // lock (RegisterLock)
  122. // {
  123. // if (action != null)
  124. // {
  125. // if (!actions.ContainsKey(name))
  126. // {
  127. // actions.TryAdd(name, new Delegation() { FuncPar = action });
  128. // }
  129. // }
  130. // }
  131. //}
  132. }
  133. internal class Delegation
  134. {
  135. public Action<object> ActionObj { get; set; }
  136. public Action ActionBus { get; set; }
  137. public Action CallBack { get; set; }
  138. public Func<object> FuncObj { get; set; }
  139. public Func<object, object> FuncPar { get; set; }
  140. }
  141. }