From 924e847024266fcce8d258d4b240b25c3ed7de85 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com>
Date: Wed, 2 Nov 2022 09:42:42 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=97=A5=E5=BF=97=E4=BF=A1?=
=?UTF-8?q?=E6=81=AF=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HKCardIN/HKCardIN.csproj | 1 +
HKCardIN/Helper/HKHelper.cs | 3 +-
HKCardIN/Helper/ThreadManage.cs | 890 +++++++++++-----------
HKCardIN/Logic/BaseLogic.cs | 8 +-
HKCardIN/ViewModels/RootViewModel.cs | 3 +-
HKCardOUT/Bootstrapper.cs | 7 +-
HKCardOUT/HKCardOUT.csproj | 1 +
HKCardOUT/Helper/HKHelpers.cs | 7 +-
HKCardOUT/Helper/ThreadManage.cs | 890 +++++++++++-----------
HKCardOUT/ViewModels/AdWindowViewModel.cs | 3 +-
HKCardOUT/ViewModels/RootViewModel.cs | 12 +-
HKCardOUT/Views/AdWindow.xaml.cs | 13 +-
HKControl/HKControl.csproj | 2 +-
HKControl/Main.cs | 23 +-
HKHelper/HKHelper.cs | 19 +
HKHelper/Json.cs | 144 ++--
HKHelper/ThreadManage.cs | 890 +++++++++++-----------
17 files changed, 1467 insertions(+), 1449 deletions(-)
diff --git a/HKCardIN/HKCardIN.csproj b/HKCardIN/HKCardIN.csproj
index 49f47cb..8982fe1 100644
--- a/HKCardIN/HKCardIN.csproj
+++ b/HKCardIN/HKCardIN.csproj
@@ -30,6 +30,7 @@
+
diff --git a/HKCardIN/Helper/HKHelper.cs b/HKCardIN/Helper/HKHelper.cs
index ceb98bf..f89ab1b 100644
--- a/HKCardIN/Helper/HKHelper.cs
+++ b/HKCardIN/Helper/HKHelper.cs
@@ -1,4 +1,5 @@
-using System;
+using BPA.Helper;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
diff --git a/HKCardIN/Helper/ThreadManage.cs b/HKCardIN/Helper/ThreadManage.cs
index c389a3f..eea8c9f 100644
--- a/HKCardIN/Helper/ThreadManage.cs
+++ b/HKCardIN/Helper/ThreadManage.cs
@@ -1,445 +1,445 @@
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace HKCardIN.Helper
-{
- public class Singleton where T : new()
- {
- private static object _async = new object();
-
- private static T _instance;
-
- static readonly Lazy instance = new();
- ///
- /// 获取实例
- ///
- ///
- public static T GetInstance()
- {
- return instance.Value;
- }
- }
- public class ThreadManage : Singleton
- {
- string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
- private static readonly object _lock = new object();
- StringBuilder callbackKey = new StringBuilder();
- List keys = new List();
- ConcurrentDictionary Threads = new ConcurrentDictionary();
- ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
-
- ///
- /// 停止指定任务
- ///
- /// 任务名
- /// 任务结束的回调
- public void StopTask(string key, Action ExitCallback = null)
- {
- if (CancellationTokenSources.ContainsKey(guid + key))
- {
- CancellationTokenSources[guid + key]?.Cancel();
- ActionManage.GetInstance.Register(ExitCallback, guid + key);
- }
- else
- {
- if (ExitCallback != null) ExitCallback();
- }
- }
-
- ///
- /// 长任务,带 while true 的循环
- ///
- ///
- ///
- public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- ReStart:
- try
- {
- while (!CancellationTokenSources[guid + key].IsCancellationRequested)
- {
- if (action != null) action();
- }
- }
- catch (Exception ex)
- {
- if (IsRestart)
- {
- Thread.Sleep(2000);
- goto ReStart;
- }
- else
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
- Threads.TryRemove(guid + key, out Task temp1);
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- if (RunComplete != null) RunComplete();
- }), guid + key));
- }
-
-
- ///
- /// 不带 while true 的循环任务
- ///
- ///
- ///
- public void Start(Action action, string key, bool isRestart = false)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- try
- {
- if (action != null) action();
- }
- catch (Exception ex)
- {
- if (isRestart)
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
- Threads.TryRemove(guid + key, out Task item2);
- Start(action, key, isRestart);
- }
- else
- {
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- }), guid + key));
- }
-
- private void ThreadStatus(Task task, string key)
- {
- bool IsRemove = false;
- string name = key.Substring(key.LastIndexOf('-') + 1);
- switch (task.Status)
- {
- case TaskStatus.RanToCompletion:
- IsRemove = true;
- break;
- case TaskStatus.Faulted:
- IsRemove = true;
- break;
- case TaskStatus.Canceled:
- IsRemove = true;
- break;
- default:
- break;
- }
-
- if (IsRemove)
- {
- if (Threads.ContainsKey(key))
- Threads.TryRemove(key, out Task t);
- if (CancellationTokenSources.ContainsKey(key))
- CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
- ActionManage.GetInstance.Send(key);
- }
- }
-
- ///
- /// 释放所有线程资源
- ///
- public void Dispose()
- {
- for (int i = 0; i < CancellationTokenSources.Count; i++)
- {
- CancellationTokenSources.ElementAt(i).Value.Cancel();
- }
- }
-
- ///
- /// 判断指定线程是否完成
- ///
- ///
- ///
- public bool IsComplete(string key)
- {
- if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
- return false;
- }
-
- }
- internal class Delegation
- {
- ///
- /// 带参数的委托
- ///
- public Action
+
diff --git a/HKCardOUT/Helper/HKHelpers.cs b/HKCardOUT/Helper/HKHelpers.cs
index fe280fe..3a9b632 100644
--- a/HKCardOUT/Helper/HKHelpers.cs
+++ b/HKCardOUT/Helper/HKHelpers.cs
@@ -1,4 +1,5 @@
-using System;
+using BPA.Helper;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
@@ -8,7 +9,7 @@ using System.Threading.Tasks;
namespace HKCardOUT.Helper
{
- public class HKHelpers: Singleton
+ public class HKHelpers : Singleton
{
///
/// 判断网络状况的方法,返回值true为连接,false为未连接
@@ -27,5 +28,5 @@ namespace HKCardOUT.Helper
return InternetGetConnectedState(out int i, 0);
}
- }
}
+}
diff --git a/HKCardOUT/Helper/ThreadManage.cs b/HKCardOUT/Helper/ThreadManage.cs
index fba56d7..ee1ebae 100644
--- a/HKCardOUT/Helper/ThreadManage.cs
+++ b/HKCardOUT/Helper/ThreadManage.cs
@@ -1,445 +1,445 @@
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace HKCardOUT.Helper
-{
- public class Singleton where T : new()
- {
- private static object _async = new object();
-
- private static T _instance;
-
- static readonly Lazy instance = new();
- ///
- /// 获取实例
- ///
- ///
- public static T GetInstance()
- {
- return instance.Value;
- }
- }
- public class ThreadManage : Singleton
- {
- string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
- private static readonly object _lock = new object();
- StringBuilder callbackKey = new StringBuilder();
- List keys = new List();
- ConcurrentDictionary Threads = new ConcurrentDictionary();
- ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
-
- ///
- /// 停止指定任务
- ///
- /// 任务名
- /// 任务结束的回调
- public void StopTask(string key, Action ExitCallback = null)
- {
- if (CancellationTokenSources.ContainsKey(guid + key))
- {
- CancellationTokenSources[guid + key]?.Cancel();
- ActionManage.GetInstance.Register(ExitCallback, guid + key);
- }
- else
- {
- if (ExitCallback != null) ExitCallback();
- }
- }
-
- ///
- /// 长任务,带 while true 的循环
- ///
- ///
- ///
- public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- ReStart:
- try
- {
- while (!CancellationTokenSources[guid + key].IsCancellationRequested)
- {
- if (action != null) action();
- }
- }
- catch (Exception ex)
- {
- if (IsRestart)
- {
- Thread.Sleep(2000);
- goto ReStart;
- }
- else
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
- Threads.TryRemove(guid + key, out Task temp1);
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- if (RunComplete != null) RunComplete();
- }), guid + key));
- }
-
-
- ///
- /// 不带 while true 的循环任务
- ///
- ///
- ///
- public void Start(Action action, string key, bool isRestart = false)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- try
- {
- if (action != null) action();
- }
- catch (Exception ex)
- {
- if (isRestart)
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
- Threads.TryRemove(guid + key, out Task item2);
- Start(action, key, isRestart);
- }
- else
- {
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- }), guid + key));
- }
-
- private void ThreadStatus(Task task, string key)
- {
- bool IsRemove = false;
- string name = key.Substring(key.LastIndexOf('-') + 1);
- switch (task.Status)
- {
- case TaskStatus.RanToCompletion:
- IsRemove = true;
- break;
- case TaskStatus.Faulted:
- IsRemove = true;
- break;
- case TaskStatus.Canceled:
- IsRemove = true;
- break;
- default:
- break;
- }
-
- if (IsRemove)
- {
- if (Threads.ContainsKey(key))
- Threads.TryRemove(key, out Task t);
- if (CancellationTokenSources.ContainsKey(key))
- CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
- ActionManage.GetInstance.Send(key);
- }
- }
-
- ///
- /// 释放所有线程资源
- ///
- public void Dispose()
- {
- for (int i = 0; i < CancellationTokenSources.Count; i++)
- {
- CancellationTokenSources.ElementAt(i).Value.Cancel();
- }
- }
-
- ///
- /// 判断指定线程是否完成
- ///
- ///
- ///
- public bool IsComplete(string key)
- {
- if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
- return false;
- }
-
- }
- 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; }
- }
- 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);
- }
-
- ///
- /// 执行注册过的委托
- ///
- /// 注册委托的key
- /// 委托参数
- /// 委托回调
- public void Send(string key, object[] par, Action Callback = null)
- {
- lock (SendLock)
- 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 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 void Register(T action, string key)
- {
- lock (RegisterLock)
- {
- if (action != null)
- {
- 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 });
- }
- }
- }
-
- }
-
- }
- public static class ExpandMethod
- {
- ///
- /// 获取布尔数组指定值得索引
- ///
- /// 要获取索引的数组
- /// 要获取索引的值
- ///
- public static int GetIndex(this bool[] obj, bool value)
- {
- if (obj == null) return -1;
- return Array.FindIndex(obj, p => p == value);
- }
-
- ///
- /// 获取字符串数组指定值得索引
- ///
- /// 要获取索引的数组
- /// 要获取索引的值
- ///
- public static int GetIndex(this string[] obj, string value)
- {
- if (obj == null || value == null) return -1;
- return Array.FindIndex(obj, p => p == value && p.Length > 0);
- }
-
- ///
- /// 委托回调
- ///
- /// 要执行的委托
- /// 委托回调
- public static void Invoke(this Action action, Action callback)
- {
- action?.Invoke();
- callback?.Invoke();
- }
-
- ///
- /// 委托回调
- ///
- /// 要执行的委托
- /// 要执行的委托的参数
- /// 委托回调
- public static void Invoke(this Action action, object par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
- public static void Invokes(this Action action, object[] par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
-
- ///
- /// 字节数组转换成32位整数
- ///
- ///
- ///
- public static int BytesToInt(this byte[] bytes)
- {
- if (bytes.Length > 4) return -1;
- int ReturnVlaue = 0;
- for (int i = 0; i < bytes.Length; i++)
- {
- ReturnVlaue += (int)(bytes[i] << (i * 8));
- }
- return ReturnVlaue;
- }
-
- ///
- /// 字节数组转换成 ushort 数组
- ///
- /// 要转换的字节数组
- /// 字节高度顺序控制
- ///
- public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
- {
- int len = bytes.Length;
-
- byte[] srcPlus = new byte[len + 1];
- bytes.CopyTo(srcPlus, 0);
- int count = len >> 1;
-
- if (len % 2 != 0)
- {
- count += 1;
- }
-
- ushort[] dest = new ushort[count];
- if (reverse)
- {
- for (int i = 0; i < count; i++)
- {
- dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
- }
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
- }
- }
-
- return dest;
- }
-
- ///
- /// ushort 数组转换成字节数组
- ///
- /// 需要转换的 ushort数组
- /// 高低字节的设置
- ///
- public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
- {
-
- int count = src.Length;
- byte[] dest = new byte[count << 1];
- if (reverse)
- {
- for (int i = 0; i < count; i++)
- {
- dest[i * 2] = (byte)(src[i] >> 8);
- dest[i * 2 + 1] = (byte)(src[i] >> 0);
- }
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- dest[i * 2] = (byte)(src[i] >> 0);
- dest[i * 2 + 1] = (byte)(src[i] >> 8);
- }
- }
- return dest;
- }
- }
-}
+//using System;
+//using System.Collections.Concurrent;
+//using System.Collections.Generic;
+//using System.Linq;
+//using System.Text;
+//using System.Threading;
+//using System.Threading.Tasks;
+
+//namespace HKCardOUT.Helper
+//{
+// public class Singleton where T : new()
+// {
+// private static object _async = new object();
+
+// private static T _instance;
+
+// static readonly Lazy instance = new();
+// ///
+// /// 获取实例
+// ///
+// ///
+// public static T GetInstance()
+// {
+// return instance.Value;
+// }
+// }
+// public class ThreadManage : Singleton
+// {
+// string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
+// private static readonly object _lock = new object();
+// StringBuilder callbackKey = new StringBuilder();
+// List keys = new List();
+// ConcurrentDictionary Threads = new ConcurrentDictionary();
+// ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
+
+// ///
+// /// 停止指定任务
+// ///
+// /// 任务名
+// /// 任务结束的回调
+// public void StopTask(string key, Action ExitCallback = null)
+// {
+// if (CancellationTokenSources.ContainsKey(guid + key))
+// {
+// CancellationTokenSources[guid + key]?.Cancel();
+// ActionManage.GetInstance.Register(ExitCallback, guid + key);
+// }
+// else
+// {
+// if (ExitCallback != null) ExitCallback();
+// }
+// }
+
+// ///
+// /// 长任务,带 while true 的循环
+// ///
+// ///
+// ///
+// public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
+// {
+// CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
+// bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
+// {
+// Thread.CurrentThread.Name = key;
+// ReStart:
+// try
+// {
+// while (!CancellationTokenSources[guid + key].IsCancellationRequested)
+// {
+// if (action != null) action();
+// }
+// }
+// catch (Exception ex)
+// {
+// if (IsRestart)
+// {
+// Thread.Sleep(2000);
+// goto ReStart;
+// }
+// else
+// {
+// CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
+// Threads.TryRemove(guid + key, out Task temp1);
+// }
+// }
+// }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
+// {
+// ThreadStatus(t, o.ToString());
+// if (RunComplete != null) RunComplete();
+// }), guid + key));
+// }
+
+
+// ///
+// /// 不带 while true 的循环任务
+// ///
+// ///
+// ///
+// public void Start(Action action, string key, bool isRestart = false)
+// {
+// CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
+// bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
+// {
+// Thread.CurrentThread.Name = key;
+// try
+// {
+// if (action != null) action();
+// }
+// catch (Exception ex)
+// {
+// if (isRestart)
+// {
+// CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
+// Threads.TryRemove(guid + key, out Task item2);
+// Start(action, key, isRestart);
+// }
+// else
+// {
+// }
+// }
+// }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
+// {
+// ThreadStatus(t, o.ToString());
+// }), guid + key));
+// }
+
+// private void ThreadStatus(Task task, string key)
+// {
+// bool IsRemove = false;
+// string name = key.Substring(key.LastIndexOf('-') + 1);
+// switch (task.Status)
+// {
+// case TaskStatus.RanToCompletion:
+// IsRemove = true;
+// break;
+// case TaskStatus.Faulted:
+// IsRemove = true;
+// break;
+// case TaskStatus.Canceled:
+// IsRemove = true;
+// break;
+// default:
+// break;
+// }
+
+// if (IsRemove)
+// {
+// if (Threads.ContainsKey(key))
+// Threads.TryRemove(key, out Task t);
+// if (CancellationTokenSources.ContainsKey(key))
+// CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
+// ActionManage.GetInstance.Send(key);
+// }
+// }
+
+// ///
+// /// 释放所有线程资源
+// ///
+// public void Dispose()
+// {
+// for (int i = 0; i < CancellationTokenSources.Count; i++)
+// {
+// CancellationTokenSources.ElementAt(i).Value.Cancel();
+// }
+// }
+
+// ///
+// /// 判断指定线程是否完成
+// ///
+// ///
+// ///
+// public bool IsComplete(string key)
+// {
+// if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
+// return false;
+// }
+
+// }
+// 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; }
+// }
+// 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);
+// }
+
+// ///
+// /// 执行注册过的委托
+// ///
+// /// 注册委托的key
+// /// 委托参数
+// /// 委托回调
+// public void Send(string key, object[] par, Action Callback = null)
+// {
+// lock (SendLock)
+// 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 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 void Register(T action, string key)
+// {
+// lock (RegisterLock)
+// {
+// if (action != null)
+// {
+// 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 });
+// }
+// }
+// }
+
+// }
+
+// }
+// public static class ExpandMethod
+// {
+// ///
+// /// 获取布尔数组指定值得索引
+// ///
+// /// 要获取索引的数组
+// /// 要获取索引的值
+// ///
+// public static int GetIndex(this bool[] obj, bool value)
+// {
+// if (obj == null) return -1;
+// return Array.FindIndex(obj, p => p == value);
+// }
+
+// ///
+// /// 获取字符串数组指定值得索引
+// ///
+// /// 要获取索引的数组
+// /// 要获取索引的值
+// ///
+// public static int GetIndex(this string[] obj, string value)
+// {
+// if (obj == null || value == null) return -1;
+// return Array.FindIndex(obj, p => p == value && p.Length > 0);
+// }
+
+// ///
+// /// 委托回调
+// ///
+// /// 要执行的委托
+// /// 委托回调
+// public static void Invoke(this Action action, Action callback)
+// {
+// action?.Invoke();
+// callback?.Invoke();
+// }
+
+// ///
+// /// 委托回调
+// ///
+// /// 要执行的委托
+// /// 要执行的委托的参数
+// /// 委托回调
+// public static void Invoke(this Action action, object par, Action callback)
+// {
+// action?.Invoke(par);
+// callback?.Invoke();
+// }
+
+// public static void Invokes(this Action action, object[] par, Action callback)
+// {
+// action?.Invoke(par);
+// callback?.Invoke();
+// }
+
+
+// ///
+// /// 字节数组转换成32位整数
+// ///
+// ///
+// ///
+// public static int BytesToInt(this byte[] bytes)
+// {
+// if (bytes.Length > 4) return -1;
+// int ReturnVlaue = 0;
+// for (int i = 0; i < bytes.Length; i++)
+// {
+// ReturnVlaue += (int)(bytes[i] << (i * 8));
+// }
+// return ReturnVlaue;
+// }
+
+// ///
+// /// 字节数组转换成 ushort 数组
+// ///
+// /// 要转换的字节数组
+// /// 字节高度顺序控制
+// ///
+// public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
+// {
+// int len = bytes.Length;
+
+// byte[] srcPlus = new byte[len + 1];
+// bytes.CopyTo(srcPlus, 0);
+// int count = len >> 1;
+
+// if (len % 2 != 0)
+// {
+// count += 1;
+// }
+
+// ushort[] dest = new ushort[count];
+// if (reverse)
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
+// }
+// }
+// else
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
+// }
+// }
+
+// return dest;
+// }
+
+// ///
+// /// ushort 数组转换成字节数组
+// ///
+// /// 需要转换的 ushort数组
+// /// 高低字节的设置
+// ///
+// public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
+// {
+
+// int count = src.Length;
+// byte[] dest = new byte[count << 1];
+// if (reverse)
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i * 2] = (byte)(src[i] >> 8);
+// dest[i * 2 + 1] = (byte)(src[i] >> 0);
+// }
+// }
+// else
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i * 2] = (byte)(src[i] >> 0);
+// dest[i * 2 + 1] = (byte)(src[i] >> 8);
+// }
+// }
+// return dest;
+// }
+// }
+//}
diff --git a/HKCardOUT/ViewModels/AdWindowViewModel.cs b/HKCardOUT/ViewModels/AdWindowViewModel.cs
index 7fedb0b..e3be0c4 100644
--- a/HKCardOUT/ViewModels/AdWindowViewModel.cs
+++ b/HKCardOUT/ViewModels/AdWindowViewModel.cs
@@ -1,4 +1,5 @@
-using DTO;
+using BPA.Helper;
+using DTO;
using HKCardOUT.Helper;
using HKCardOUT.Logic.Service;
using Microsoft.VisualBasic.ApplicationServices;
diff --git a/HKCardOUT/ViewModels/RootViewModel.cs b/HKCardOUT/ViewModels/RootViewModel.cs
index 11be84a..31ae7a0 100644
--- a/HKCardOUT/ViewModels/RootViewModel.cs
+++ b/HKCardOUT/ViewModels/RootViewModel.cs
@@ -1,4 +1,5 @@
-using DTO;
+using BPA.Helper;
+using DTO;
using HKCardOUT.Helper;
using HKCardOUT.Logic.Model;
using HKCardOUT.Logic.Service;
@@ -88,8 +89,9 @@ namespace HKCardOUT.ViewModels
if (res != null)
{
HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 卡号地址:{res.Address}----------卡号数据:{res.ResData}");
- if (!res.ResData.IsNullOrEmpty()) {
- if (res.ResData.Length==20)
+ if (!res.ResData.IsNullOrEmpty())
+ {
+ if (res.ResData.Length == 20)
ReadFunc?.Invoke(res);
}
Thread.Sleep(2000);
@@ -176,7 +178,7 @@ namespace HKCardOUT.ViewModels
var SC = System.Windows.Forms.Screen.AllScreens.Count();
for (int i = 0; i < SC; i++)
{
- var win = new AdWindow(Ad[i].Ad,i);
+ var win = new AdWindow(Ad[i].Ad, i);
win.DataContext = new AdWindowViewModel();
var rectangle = System.Windows.Forms.Screen.AllScreens[i].WorkingArea;
win.Height = rectangle.Height;
@@ -190,7 +192,7 @@ namespace HKCardOUT.ViewModels
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
- var win = new AdWindow("暂无广告",0);
+ var win = new AdWindow("暂无广告", 0);
var rectangle = System.Windows.Forms.Screen.AllScreens[0].WorkingArea;
win.Height = rectangle.Height;
win.Width = rectangle.Width;
diff --git a/HKCardOUT/Views/AdWindow.xaml.cs b/HKCardOUT/Views/AdWindow.xaml.cs
index e407ba5..b048816 100644
--- a/HKCardOUT/Views/AdWindow.xaml.cs
+++ b/HKCardOUT/Views/AdWindow.xaml.cs
@@ -14,6 +14,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
+using BPA.Helper;
namespace HKCardOUT.Views
{
@@ -26,6 +27,14 @@ namespace HKCardOUT.Views
public AdWindow(string input, int bindScreen)
{
InitializeComponent();
+ MessageLog.GetInstance.NotifyShow = new Action((s) =>
+ {
+ HKLog.HKLogImport.WriteInfo(s);
+ });
+ MessageLog.GetInstance.NotifyShowEx = new Action((s) =>
+ {
+ HKLog.HKLogImport.WriteInfo(s);
+ });
this.Loaded += (sender, ev) =>
{
this.WindowState = WindowState.Maximized;
@@ -40,7 +49,7 @@ namespace HKCardOUT.Views
};
InitView2(input);
BindScreen = bindScreen;
-
+
}
@@ -54,6 +63,6 @@ namespace HKCardOUT.Views
view2.CoreWebView2.Settings.AreDevToolsEnabled = false;
view2.CoreWebView2.NavigateToString(html);
}
-
+
}
}
diff --git a/HKControl/HKControl.csproj b/HKControl/HKControl.csproj
index b697c9f..ba6b298 100644
--- a/HKControl/HKControl.csproj
+++ b/HKControl/HKControl.csproj
@@ -8,11 +8,11 @@
+
-
diff --git a/HKControl/Main.cs b/HKControl/Main.cs
index 4975b02..4436985 100644
--- a/HKControl/Main.cs
+++ b/HKControl/Main.cs
@@ -1,6 +1,6 @@
using System.Collections.Concurrent;
using System.Diagnostics;
-using HKHelper;
+using BPA.Helper;
using S7.Net;
namespace HKControl
@@ -26,22 +26,16 @@ namespace HKControl
switch (CarNum)
{
case 1:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[1].LeftWindowData.IsSwipe;
case 2:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[1].RightWindowData.IsSwipe;
case 3:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[2].LeftWindowData.IsSwipe;
case 4:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[2].RightWindowData.IsSwipe;
case 5:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[3].LeftWindowData.IsSwipe;
case 6:
- //Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[3].LeftWindowData.IsSwipe;
default:
break;
@@ -89,15 +83,6 @@ namespace HKControl
{
HKLog.HKLogImport.WriteInfo(ex.ToString());
}
-
-
-
- //DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read("M7.0");
- //DataModels[item.DeviceNum].LeftWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read("M7.1");
-
- //DataModels[item.DeviceNum].RightWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read("M7.2");
- //DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read("M7.3");
-
Thread.Sleep(100);
}), $"{item.DeviceNum} 号设备监听", true);
});
@@ -119,27 +104,21 @@ namespace HKControl
switch (carNum)
{
case 1:
- //SiemensDicitonary[1].Write("M6.0", true);
Control(1, "M6.1");
break;
case 2:
- //SiemensDicitonary[1].Write("M6.1", true);
Control(1, "M6.0");
break;
case 3:
- //SiemensDicitonary[2].Write("M6.0", true);
Control(2, "M6.1");
break;
case 4:
- //SiemensDicitonary[2].Write("M6.1", true);
Control(2, "M6.0");
break;
case 5:
- //SiemensDicitonary[3].Write("M6.0", true);
Control(3, "M6.1");
break;
case 6:
- //SiemensDicitonary[3].Write("M6.1", true);
Control(3, "M6.0");
break;
diff --git a/HKHelper/HKHelper.cs b/HKHelper/HKHelper.cs
index 294c8df..49249a6 100644
--- a/HKHelper/HKHelper.cs
+++ b/HKHelper/HKHelper.cs
@@ -8,6 +8,25 @@ using System.Threading.Tasks;
namespace HKHelper
{
+
+ public class Singleton where T : new()
+ {
+ private static object _async = new object();
+
+ private static T _instance;
+
+ static readonly Lazy instance = new();
+ ///
+ /// 获取实例
+ ///
+ ///
+ public static T GetInstance()
+ {
+ return instance.Value;
+ }
+ }
+
+
public class HKHelper : Singleton
{
///
diff --git a/HKHelper/Json.cs b/HKHelper/Json.cs
index 70e9d23..f7615f8 100644
--- a/HKHelper/Json.cs
+++ b/HKHelper/Json.cs
@@ -1,81 +1,81 @@
-using Newtonsoft.Json;
-using System;
-using System.IO;
-using System.Collections.Concurrent;
-using System.Reflection;
+//using Newtonsoft.Json;
+//using System;
+//using System.IO;
+//using System.Collections.Concurrent;
+//using System.Reflection;
-namespace HKHelper
-{
- ///
- /// Json参数服务类
- ///
- public class Json where T : class, new()
- {
- static string path
- {
- get
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\JSON"));
- return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\JSON\\{typeof(T).Name}.json";
- }
- }
+//namespace HKHelper
+//{
+// ///
+// /// Json参数服务类
+// ///
+// public class Json where T : class, new()
+// {
+// static string path
+// {
+// get
+// {
+// Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\JSON"));
+// return $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\JSON\\{typeof(T).Name}.json";
+// }
+// }
- public static T Data { get; set; } = new T();
+// public static T Data { get; set; } = new T();
- ///
- /// 保存数据
- ///
- public static void Save()
- {
- string outjson = JsonConvert.SerializeObject(Data);
- File.WriteAllText(path, outjson);
- }
+// ///
+// /// 保存数据
+// ///
+// public static void Save()
+// {
+// string outjson = JsonConvert.SerializeObject(Data);
+// File.WriteAllText(path, outjson);
+// }
- ///
- /// 获取保存的数据
- ///
- public static void Read()
- {
- if (File.Exists(path))
- {
- string JsonString = File.ReadAllText(path);
- var result = JsonConvert.DeserializeObject(JsonString);
- if (result != null) { Data = result; }
- }
- }
+// ///
+// /// 获取保存的数据
+// ///
+// public static void Read()
+// {
+// if (File.Exists(path))
+// {
+// string JsonString = File.ReadAllText(path);
+// var result = JsonConvert.DeserializeObject(JsonString);
+// if (result != null) { Data = result; }
+// }
+// }
- ///
- /// 保存带接口的对象
- ///
- public static void SaveInterface()
- {
- var settings = new JsonSerializerSettings();
- settings.TypeNameHandling = TypeNameHandling.Objects;
- string outjson = JsonConvert.SerializeObject(Data, Formatting.Indented, settings);
- File.WriteAllText(path, outjson);
- }
+// ///
+// /// 保存带接口的对象
+// ///
+// public static void SaveInterface()
+// {
+// var settings = new JsonSerializerSettings();
+// settings.TypeNameHandling = TypeNameHandling.Objects;
+// string outjson = JsonConvert.SerializeObject(Data, Formatting.Indented, settings);
+// File.WriteAllText(path, outjson);
+// }
- ///
- /// 获取带接口对象的字符串
- ///
- public static void ReadInterface()
- {
- if (File.Exists(path))
- {
- var settings = new JsonSerializerSettings();
- settings.TypeNameHandling = TypeNameHandling.Objects;
- string JsonString = File.ReadAllText(path);
- var result = JsonConvert.DeserializeObject(JsonString, settings);
- if (result != null) { Data = result; }
- }
- }
+// ///
+// /// 获取带接口对象的字符串
+// ///
+// public static void ReadInterface()
+// {
+// if (File.Exists(path))
+// {
+// var settings = new JsonSerializerSettings();
+// settings.TypeNameHandling = TypeNameHandling.Objects;
+// string JsonString = File.ReadAllText(path);
+// var result = JsonConvert.DeserializeObject(JsonString, settings);
+// if (result != null) { Data = result; }
+// }
+// }
- /*
- 使用反序列化接口对象的方法
- 一、使用 SaveInterface 方法保存成字符串,使用 ReadInterface 方法获取对象
- 二、在接口属性上加一个特性 [JsonProperty(TypeNameHandling = TypeNameHandling.Auto)]
- */
+// /*
+// 使用反序列化接口对象的方法
+// 一、使用 SaveInterface 方法保存成字符串,使用 ReadInterface 方法获取对象
+// 二、在接口属性上加一个特性 [JsonProperty(TypeNameHandling = TypeNameHandling.Auto)]
+// */
- }
-}
+// }
+//}
diff --git a/HKHelper/ThreadManage.cs b/HKHelper/ThreadManage.cs
index 423c0f0..abed5c3 100644
--- a/HKHelper/ThreadManage.cs
+++ b/HKHelper/ThreadManage.cs
@@ -1,445 +1,445 @@
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace HKHelper
-{
- public class Singleton where T : new()
- {
- private static object _async = new object();
-
- private static T _instance;
-
- static readonly Lazy instance = new();
- ///
- /// 获取实例
- ///
- ///
- public static T GetInstance()
- {
- return instance.Value;
- }
- }
- public class ThreadManage : Singleton
- {
- string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
- private static readonly object _lock = new object();
- StringBuilder callbackKey = new StringBuilder();
- List keys = new List();
- ConcurrentDictionary Threads = new ConcurrentDictionary();
- ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
-
- ///
- /// 停止指定任务
- ///
- /// 任务名
- /// 任务结束的回调
- public void StopTask(string key, Action ExitCallback = null)
- {
- if (CancellationTokenSources.ContainsKey(guid + key))
- {
- CancellationTokenSources[guid + key]?.Cancel();
- ActionManage.GetInstance.Register(ExitCallback, guid + key);
- }
- else
- {
- if (ExitCallback != null) ExitCallback();
- }
- }
-
- ///
- /// 长任务,带 while true 的循环
- ///
- ///
- ///
- public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- ReStart:
- try
- {
- while (!CancellationTokenSources[guid + key].IsCancellationRequested)
- {
- if (action != null) action();
- }
- }
- catch (Exception ex)
- {
- if (IsRestart)
- {
- Thread.Sleep(2000);
- goto ReStart;
- }
- else
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
- Threads.TryRemove(guid + key, out Task temp1);
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- if (RunComplete != null) RunComplete();
- }), guid + key));
- }
-
-
- ///
- /// 不带 while true 的循环任务
- ///
- ///
- ///
- public void Start(Action action, string key, bool isRestart = false)
- {
- CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
- bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
- {
- Thread.CurrentThread.Name = key;
- try
- {
- if (action != null) action();
- }
- catch (Exception ex)
- {
- if (isRestart)
- {
- CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
- Threads.TryRemove(guid + key, out Task item2);
- Start(action, key, isRestart);
- }
- else
- {
- }
- }
- }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
- {
- ThreadStatus(t, o.ToString());
- }), guid + key));
- }
-
- private void ThreadStatus(Task task, string key)
- {
- bool IsRemove = false;
- string name = key.Substring(key.LastIndexOf('-') + 1);
- switch (task.Status)
- {
- case TaskStatus.RanToCompletion:
- IsRemove = true;
- break;
- case TaskStatus.Faulted:
- IsRemove = true;
- break;
- case TaskStatus.Canceled:
- IsRemove = true;
- break;
- default:
- break;
- }
-
- if (IsRemove)
- {
- if (Threads.ContainsKey(key))
- Threads.TryRemove(key, out Task t);
- if (CancellationTokenSources.ContainsKey(key))
- CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
- ActionManage.GetInstance.Send(key);
- }
- }
-
- ///
- /// 释放所有线程资源
- ///
- public void Dispose()
- {
- for (int i = 0; i < CancellationTokenSources.Count; i++)
- {
- CancellationTokenSources.ElementAt(i).Value.Cancel();
- }
- }
-
- ///
- /// 判断指定线程是否完成
- ///
- ///
- ///
- public bool IsComplete(string key)
- {
- if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
- return false;
- }
-
- }
- 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; }
- }
- 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);
- }
-
- ///
- /// 执行注册过的委托
- ///
- /// 注册委托的key
- /// 委托参数
- /// 委托回调
- public void Send(string key, object[] par, Action Callback = null)
- {
- lock (SendLock)
- 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 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 void Register(T action, string key)
- {
- lock (RegisterLock)
- {
- if (action != null)
- {
- 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 });
- }
- }
- }
-
- }
-
- }
- public static class ExpandMethod
- {
- ///
- /// 获取布尔数组指定值得索引
- ///
- /// 要获取索引的数组
- /// 要获取索引的值
- ///
- public static int GetIndex(this bool[] obj, bool value)
- {
- if (obj == null) return -1;
- return Array.FindIndex(obj, p => p == value);
- }
-
- ///
- /// 获取字符串数组指定值得索引
- ///
- /// 要获取索引的数组
- /// 要获取索引的值
- ///
- public static int GetIndex(this string[] obj, string value)
- {
- if (obj == null || value == null) return -1;
- return Array.FindIndex(obj, p => p == value && p.Length > 0);
- }
-
- ///
- /// 委托回调
- ///
- /// 要执行的委托
- /// 委托回调
- public static void Invoke(this Action action, Action callback)
- {
- action?.Invoke();
- callback?.Invoke();
- }
-
- ///
- /// 委托回调
- ///
- /// 要执行的委托
- /// 要执行的委托的参数
- /// 委托回调
- public static void Invoke(this Action action, object par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
- public static void Invokes(this Action action, object[] par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
-
- ///
- /// 字节数组转换成32位整数
- ///
- ///
- ///
- public static int BytesToInt(this byte[] bytes)
- {
- if (bytes.Length > 4) return -1;
- int ReturnVlaue = 0;
- for (int i = 0; i < bytes.Length; i++)
- {
- ReturnVlaue += (int)(bytes[i] << (i * 8));
- }
- return ReturnVlaue;
- }
-
- ///
- /// 字节数组转换成 ushort 数组
- ///
- /// 要转换的字节数组
- /// 字节高度顺序控制
- ///
- public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
- {
- int len = bytes.Length;
-
- byte[] srcPlus = new byte[len + 1];
- bytes.CopyTo(srcPlus, 0);
- int count = len >> 1;
-
- if (len % 2 != 0)
- {
- count += 1;
- }
-
- ushort[] dest = new ushort[count];
- if (reverse)
- {
- for (int i = 0; i < count; i++)
- {
- dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
- }
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
- }
- }
-
- return dest;
- }
-
- ///
- /// ushort 数组转换成字节数组
- ///
- /// 需要转换的 ushort数组
- /// 高低字节的设置
- ///
- public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
- {
-
- int count = src.Length;
- byte[] dest = new byte[count << 1];
- if (reverse)
- {
- for (int i = 0; i < count; i++)
- {
- dest[i * 2] = (byte)(src[i] >> 8);
- dest[i * 2 + 1] = (byte)(src[i] >> 0);
- }
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- dest[i * 2] = (byte)(src[i] >> 0);
- dest[i * 2 + 1] = (byte)(src[i] >> 8);
- }
- }
- return dest;
- }
- }
-}
+//using System;
+//using System.Collections.Concurrent;
+//using System.Collections.Generic;
+//using System.Linq;
+//using System.Text;
+//using System.Threading;
+//using System.Threading.Tasks;
+
+//namespace HKHelper
+//{
+// public class Singleton where T : new()
+// {
+// private static object _async = new object();
+
+// private static T _instance;
+
+// static readonly Lazy instance = new();
+// ///
+// /// 获取实例
+// ///
+// ///
+// public static T GetInstance()
+// {
+// return instance.Value;
+// }
+// }
+// public class ThreadManage : Singleton
+// {
+// string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
+// private static readonly object _lock = new object();
+// StringBuilder callbackKey = new StringBuilder();
+// List keys = new List();
+// ConcurrentDictionary Threads = new ConcurrentDictionary();
+// ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
+
+// ///
+// /// 停止指定任务
+// ///
+// /// 任务名
+// /// 任务结束的回调
+// public void StopTask(string key, Action ExitCallback = null)
+// {
+// if (CancellationTokenSources.ContainsKey(guid + key))
+// {
+// CancellationTokenSources[guid + key]?.Cancel();
+// ActionManage.GetInstance.Register(ExitCallback, guid + key);
+// }
+// else
+// {
+// if (ExitCallback != null) ExitCallback();
+// }
+// }
+
+// ///
+// /// 长任务,带 while true 的循环
+// ///
+// ///
+// ///
+// public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
+// {
+// CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
+// bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
+// {
+// Thread.CurrentThread.Name = key;
+// ReStart:
+// try
+// {
+// while (!CancellationTokenSources[guid + key].IsCancellationRequested)
+// {
+// if (action != null) action();
+// }
+// }
+// catch (Exception ex)
+// {
+// if (IsRestart)
+// {
+// Thread.Sleep(2000);
+// goto ReStart;
+// }
+// else
+// {
+// CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
+// Threads.TryRemove(guid + key, out Task temp1);
+// }
+// }
+// }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
+// {
+// ThreadStatus(t, o.ToString());
+// if (RunComplete != null) RunComplete();
+// }), guid + key));
+// }
+
+
+// ///
+// /// 不带 while true 的循环任务
+// ///
+// ///
+// ///
+// public void Start(Action action, string key, bool isRestart = false)
+// {
+// CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
+// bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
+// {
+// Thread.CurrentThread.Name = key;
+// try
+// {
+// if (action != null) action();
+// }
+// catch (Exception ex)
+// {
+// if (isRestart)
+// {
+// CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
+// Threads.TryRemove(guid + key, out Task item2);
+// Start(action, key, isRestart);
+// }
+// else
+// {
+// }
+// }
+// }), CancellationTokenSources[guid + key].Token).ContinueWith(new Action((t, o) =>
+// {
+// ThreadStatus(t, o.ToString());
+// }), guid + key));
+// }
+
+// private void ThreadStatus(Task task, string key)
+// {
+// bool IsRemove = false;
+// string name = key.Substring(key.LastIndexOf('-') + 1);
+// switch (task.Status)
+// {
+// case TaskStatus.RanToCompletion:
+// IsRemove = true;
+// break;
+// case TaskStatus.Faulted:
+// IsRemove = true;
+// break;
+// case TaskStatus.Canceled:
+// IsRemove = true;
+// break;
+// default:
+// break;
+// }
+
+// if (IsRemove)
+// {
+// if (Threads.ContainsKey(key))
+// Threads.TryRemove(key, out Task t);
+// if (CancellationTokenSources.ContainsKey(key))
+// CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
+// ActionManage.GetInstance.Send(key);
+// }
+// }
+
+// ///
+// /// 释放所有线程资源
+// ///
+// public void Dispose()
+// {
+// for (int i = 0; i < CancellationTokenSources.Count; i++)
+// {
+// CancellationTokenSources.ElementAt(i).Value.Cancel();
+// }
+// }
+
+// ///
+// /// 判断指定线程是否完成
+// ///
+// ///
+// ///
+// public bool IsComplete(string key)
+// {
+// if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
+// return false;
+// }
+
+// }
+// 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; }
+// }
+// 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);
+// }
+
+// ///
+// /// 执行注册过的委托
+// ///
+// /// 注册委托的key
+// /// 委托参数
+// /// 委托回调
+// public void Send(string key, object[] par, Action Callback = null)
+// {
+// lock (SendLock)
+// 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 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 void Register(T action, string key)
+// {
+// lock (RegisterLock)
+// {
+// if (action != null)
+// {
+// 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 });
+// }
+// }
+// }
+
+// }
+
+// }
+// public static class ExpandMethod
+// {
+// ///
+// /// 获取布尔数组指定值得索引
+// ///
+// /// 要获取索引的数组
+// /// 要获取索引的值
+// ///
+// public static int GetIndex(this bool[] obj, bool value)
+// {
+// if (obj == null) return -1;
+// return Array.FindIndex(obj, p => p == value);
+// }
+
+// ///
+// /// 获取字符串数组指定值得索引
+// ///
+// /// 要获取索引的数组
+// /// 要获取索引的值
+// ///
+// public static int GetIndex(this string[] obj, string value)
+// {
+// if (obj == null || value == null) return -1;
+// return Array.FindIndex(obj, p => p == value && p.Length > 0);
+// }
+
+// ///
+// /// 委托回调
+// ///
+// /// 要执行的委托
+// /// 委托回调
+// public static void Invoke(this Action action, Action callback)
+// {
+// action?.Invoke();
+// callback?.Invoke();
+// }
+
+// ///
+// /// 委托回调
+// ///
+// /// 要执行的委托
+// /// 要执行的委托的参数
+// /// 委托回调
+// public static void Invoke(this Action action, object par, Action callback)
+// {
+// action?.Invoke(par);
+// callback?.Invoke();
+// }
+
+// public static void Invokes(this Action action, object[] par, Action callback)
+// {
+// action?.Invoke(par);
+// callback?.Invoke();
+// }
+
+
+// ///
+// /// 字节数组转换成32位整数
+// ///
+// ///
+// ///
+// public static int BytesToInt(this byte[] bytes)
+// {
+// if (bytes.Length > 4) return -1;
+// int ReturnVlaue = 0;
+// for (int i = 0; i < bytes.Length; i++)
+// {
+// ReturnVlaue += (int)(bytes[i] << (i * 8));
+// }
+// return ReturnVlaue;
+// }
+
+// ///
+// /// 字节数组转换成 ushort 数组
+// ///
+// /// 要转换的字节数组
+// /// 字节高度顺序控制
+// ///
+// public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
+// {
+// int len = bytes.Length;
+
+// byte[] srcPlus = new byte[len + 1];
+// bytes.CopyTo(srcPlus, 0);
+// int count = len >> 1;
+
+// if (len % 2 != 0)
+// {
+// count += 1;
+// }
+
+// ushort[] dest = new ushort[count];
+// if (reverse)
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
+// }
+// }
+// else
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
+// }
+// }
+
+// return dest;
+// }
+
+// ///
+// /// ushort 数组转换成字节数组
+// ///
+// /// 需要转换的 ushort数组
+// /// 高低字节的设置
+// ///
+// public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
+// {
+
+// int count = src.Length;
+// byte[] dest = new byte[count << 1];
+// if (reverse)
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i * 2] = (byte)(src[i] >> 8);
+// dest[i * 2 + 1] = (byte)(src[i] >> 0);
+// }
+// }
+// else
+// {
+// for (int i = 0; i < count; i++)
+// {
+// dest[i * 2] = (byte)(src[i] >> 0);
+// dest[i * 2 + 1] = (byte)(src[i] >> 8);
+// }
+// }
+// return dest;
+// }
+// }
+//}