using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.Concurrent; using System.Reflection.Metadata; using System.Threading; using System.Reflection; using BPASmartClient.Message; namespace BPASmartClient.Helper { public class ActionManage { private volatile static ActionManage _Instance; public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage()); private ActionManage() { } //private static ConcurrentDictionary actions = new ConcurrentDictionary(); private static ConcurrentDictionary actions = new ConcurrentDictionary(); static readonly object SendLock = new object(); static readonly object SendParLock = new object(); static readonly object RegisterLock = new object(); /// /// 注销委托 /// /// public void CancelRegister(string key) { if (actions.ContainsKey(key)) actions.TryRemove(key, out Delegation t); } /// /// 执行注册过的委托 /// /// 注册委托的key /// 委托参数 /// 委托回调 public void Send(string key, object par, Action Callback = null) { lock (SendLock) if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback); } public async void SendAsync(string key, object par, Action Callback = null) { await Task.Run(new Action(() => { if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback); })); } /// /// 执行注册过的委托 /// /// 注册委托的key /// 委托参数 /// 委托回调 public void Send(string key, object[] par, Action Callback = null) { lock (SendLock) if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback); } public async void SendAsync(string key, object[] par, Action Callback = null) { await Task.Run(new Action(() => { if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback); })); } /// /// 执行注册过的委托 /// /// 注册委托的key /// 委托回调 public void Send(string key, Action Callback = null) { lock (SendLock) if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback); } public async void SendAsync(string key, Action Callback = null) { await Task.Run(new Action(() => { if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback); })); } public object SendResult(string key, object par = null) { lock (SendLock) if (actions.ContainsKey(key)) if (par == null) { return actions[key].FuncObj?.Invoke(); } else { return actions[key].FuncPar?.Invoke(par); } return default; } public async Task SendResultAsync(string key, object par = null) { return await Task.Run(new Func(() => { if (actions.ContainsKey(key)) if (par == null) { return actions[key].FuncObj?.Invoke(); } else { return actions[key].FuncPar?.Invoke(par); } return default; })); } /// /// 根据 Key 注册需要执行的委托 /// /// /// 委托方法 /// 委托key /// 是否自动取消注册 public void Register(T action, string key, bool IsAutoCancelRegister = false) { lock (RegisterLock) { if (action != null) { if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d); try { if (!actions.ContainsKey(key)) { if (action is Action actionBus) actions.TryAdd(key, new Delegation() { ActionBus = actionBus }); if (action is Action actionObj) actions.TryAdd(key, new Delegation() { ActionPar = actionObj }); if (action is Action actionObjs) actions.TryAdd(key, new Delegation() { ActionPars = actionObjs }); if (action is Func funcObj) actions.TryAdd(key, new Delegation() { FuncObj = funcObj }); if (action is Func puncPar) actions.TryAdd(key, new Delegation() { FuncPar = puncPar }); } } catch (Exception ex) { Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString()); } } } } public async void RegisterAsync(T action, string key, bool IsAutoCancelRegister = false) { await Task.Run(new Action(() => { if (action != null) { if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d); try { if (!actions.ContainsKey(key)) { if (action is Action actionBus) actions.TryAdd(key, new Delegation() { ActionBus = actionBus }); if (action is Action actionObj) actions.TryAdd(key, new Delegation() { ActionPar = actionObj }); if (action is Action actionObjs) actions.TryAdd(key, new Delegation() { ActionPars = actionObjs }); if (action is Func funcObj) actions.TryAdd(key, new Delegation() { FuncObj = funcObj }); if (action is Func puncPar) actions.TryAdd(key, new Delegation() { FuncPar = puncPar }); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } })); } #region 升级版本 //private static ConcurrentDictionary MethodS = new ConcurrentDictionary(); //public void Register1(Action action, string key, bool IsAutoCancelRegister = false) //{ // MethodS.TryAdd("aa",new Action(() => { })); // lock (RegisterLock) // { // if (action != null) // { // if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d); // try // { // if (!actions.ContainsKey(key)) // { // if (action is Action actionBus) // actions.TryAdd(key, new Delegation() { ActionBus = actionBus }); // if (action is Action actionObj) // actions.TryAdd(key, new Delegation() { ActionPar = actionObj }); // if (action is Action actionObjs) // actions.TryAdd(key, new Delegation() { ActionPars = actionObjs }); // if (action is Func funcObj) // actions.TryAdd(key, new Delegation() { FuncObj = funcObj }); // if (action is Func puncPar) // actions.TryAdd(key, new Delegation() { FuncPar = puncPar }); // } // } // catch (Exception ex) // { // Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString()); // } // } // } //} #endregion } internal class Delegation { /// /// 带参数的委托 /// public Action ActionPar { get; set; } /// /// 带参数的委托 /// public Action ActionPars { get; set; } /// /// 无参数的委托 /// public Action ActionBus { get; set; } /// /// 有返回值的委托 /// public Func FuncObj { get; set; } /// /// 有返回值,有参数的委托 /// public Func FuncPar { get; set; } } }