|
|
@@ -13,7 +13,7 @@ namespace BPASmartClient.Helper |
|
|
|
/// <summary> |
|
|
|
/// 线程管理 |
|
|
|
/// </summary> |
|
|
|
public class ThreadManage:Singleton<ThreadManage> |
|
|
|
public class ThreadManage : Singleton<ThreadManage> |
|
|
|
{ |
|
|
|
string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-"; |
|
|
|
private static readonly object _lock = new object(); |
|
|
@@ -84,12 +84,27 @@ namespace BPASmartClient.Helper |
|
|
|
/// </summary> |
|
|
|
/// <param name="action"></param> |
|
|
|
/// <param name="key"></param> |
|
|
|
public void Start(Action action, string key) |
|
|
|
public void Start(Action action, string key, bool isRestart = true) |
|
|
|
{ |
|
|
|
MessageLog.GetInstance.Show($"线程 【{key}】启动"); |
|
|
|
CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource()); |
|
|
|
bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() => |
|
|
|
{ |
|
|
|
if (action != null) action(); |
|
|
|
try |
|
|
|
{ |
|
|
|
if (action != null) action(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
MessageLog.GetInstance.Show($"线程 【{key}】运行发生异常"); |
|
|
|
if (isRestart) |
|
|
|
{ |
|
|
|
MessageLog.GetInstance.Show($"线程 【{key}】正在重启"); |
|
|
|
CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1); |
|
|
|
Threads.TryRemove(guid + key, out Task item2); |
|
|
|
Start(action, key, isRestart); |
|
|
|
} |
|
|
|
} |
|
|
|
}), CancellationTokenSources[guid + key].Token).ContinueWith(new Action<Task, object>((t, o) => |
|
|
|
{ |
|
|
|
ThreadStatus(t, o.ToString()); |
|
|
|