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.
 
 

120 lines
3.8 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 ActionOperate
  10. {
  11. private volatile static ActionOperate _Instance;
  12. public static ActionOperate GetInstance => _Instance ?? (_Instance = new ActionOperate());
  13. private ActionOperate() { }
  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. /// <summary>
  20. /// 注销委托
  21. /// </summary>
  22. /// <param name="name"></param>
  23. public void CancelRegister(string name)
  24. {
  25. if (actions.ContainsKey(name))
  26. {
  27. actions.TryRemove(name, out Delegation t);
  28. }
  29. }
  30. public void Send(string name, object token, Action action = null)
  31. {
  32. lock (SendParLock)
  33. if (actions.ContainsKey(name))
  34. if (actions[name].ActionObj != null)
  35. {
  36. actions[name].ActionObj(token);
  37. if (action != null) action();
  38. }
  39. }
  40. public void Send(string name, AsyncCallback action = null)
  41. {
  42. lock (SendLock)
  43. if (actions.ContainsKey(name))
  44. if (actions[name].ActionBus != null)
  45. {
  46. if (action != null)
  47. actions[name].ActionBus.BeginInvoke(action, null);
  48. else
  49. actions[name].ActionBus();
  50. }
  51. }
  52. public object SendResult(string name, object token = null)
  53. {
  54. object result = new object();
  55. lock (SendParLock)
  56. if (actions.ContainsKey(name))
  57. if (token == null)
  58. {
  59. if (actions[name].FuncObj != null)
  60. result = actions[name].FuncObj;
  61. }
  62. else
  63. {
  64. if (actions[name].FuncPar != null)
  65. result = actions[name].FuncPar(token);
  66. }
  67. return result;
  68. }
  69. public void Register<T>(T action, string name)
  70. {
  71. lock (RegisterLock)
  72. {
  73. if (action != null)
  74. {
  75. if (!actions.ContainsKey(name))
  76. {
  77. if (action is Action actionBus)
  78. actions.TryAdd(name, new Delegation() { ActionBus = actionBus });
  79. if (action is Action<object> actionObj)
  80. actions.TryAdd(name, new Delegation() { ActionObj = actionObj });
  81. if (action is Func<object> funcObj)
  82. actions.TryAdd(name, new Delegation() { FuncObj = funcObj });
  83. if (action is Func<object, object> puncPar)
  84. actions.TryAdd(name, new Delegation() { FuncPar = puncPar });
  85. }
  86. }
  87. }
  88. }
  89. }
  90. internal class Delegation
  91. {
  92. public Action<object> ActionObj { get; set; }
  93. public Action ActionBus { get; set; }
  94. public Action CallBack { get; set; }
  95. public Func<object> FuncObj { get; set; }
  96. public Func<object, object> FuncPar { get; set; }
  97. }
  98. }