您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ActionOperate.cs 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }