using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
namespace HBLConsole.Service
{
///
/// 线程管理
///
public class ThreadManagerment
{
private volatile static ThreadManagerment _Instance;
public static ThreadManagerment GetInstance => _Instance ?? (_Instance = new ThreadManagerment());
private ThreadManagerment() { }
ConcurrentDictionary Threads = new ConcurrentDictionary();
ConcurrentDictionary CancellationTokenSources = new ConcurrentDictionary();
///
/// 停止指定任务
///
/// 任务名
/// 任务结束的回调
public void StopTask(string taskName, Action ExitCallback = null)
{
if (CancellationTokenSources.ContainsKey(taskName))
CancellationTokenSources[taskName]?.Cancel();
ActionManagerment.GetInstance.Register(ExitCallback, taskName);
}
///
/// 长任务,带 while true 的循环
///
///
///
public void StartLong(Action action, string TaskName, Action RunComplete = null)
{
CancellationTokenSources.TryAdd(TaskName, new CancellationTokenSource());
bool result = Threads.TryAdd(TaskName, Task.Factory.StartNew(new Action(() =>
{
while (!CancellationTokenSources[TaskName].IsCancellationRequested)
{
if (action != null) action();
}
}), CancellationTokenSources[TaskName].Token).ContinueWith(new Action((t, o) =>
{
ThreadStatus(t, o.ToString());
if (RunComplete != null) RunComplete();
}), TaskName));
MessageLog.GetInstance.Show($"启动线程 【{TaskName}】");
if (!result) MessageLog.GetInstance.Show($"【{TaskName}】任务已存在,请检查 TaskName");
}
///
/// 不带 while true 的循环任务
///
///
///
public void Start(Action action, string TaskName)
{
CancellationTokenSources.TryAdd(TaskName, new CancellationTokenSource());
bool result = Threads.TryAdd(TaskName, Task.Factory.StartNew(new Action(() =>
{
action();
}), CancellationTokenSources[TaskName].Token).ContinueWith(new Action((t, o) =>
{
ThreadStatus(t, o.ToString());
}), TaskName));
if (!result) MessageLog.GetInstance.Show($"【{TaskName}】任务已存在,请检查 TaskName");
}
private void ThreadStatus(Task task, string TaskName)
{
bool IsRemove = false;
switch (task.Status)
{
case TaskStatus.RanToCompletion:
MessageLog.GetInstance.Show($"线程【{TaskName}】执行完成");
IsRemove = true;
break;
case TaskStatus.Faulted:
MessageLog.GetInstance.Show($"线程【{TaskName}】执行异常,{task.Exception}");
IsRemove = true;
break;
case TaskStatus.Canceled:
MessageLog.GetInstance.Show($"线程【{TaskName}】已取消");
IsRemove = true;
break;
default:
break;
}
if (IsRemove)
{
Threads.TryRemove(Threads.FirstOrDefault(p => p.Key == TaskName.ToString()));
CancellationTokenSources.TryRemove(CancellationTokenSources.FirstOrDefault(p => p.Key == TaskName.ToString()));
ActionManagerment.GetInstance.Send(TaskName);
}
}
///
/// 释放所有线程资源
///
public void Dispose()
{
for (int i = 0; i < CancellationTokenSources.Count; i++)
{
CancellationTokenSources.ElementAt(i).Value.Cancel();
}
}
///
/// 判断指定线程是否完成
///
///
///
public bool IsComplete(string TaskName)
{
if (Threads.ContainsKey(TaskName))
{
return Threads[TaskName].IsCompleted;
}
else
{
return false;
}
}
}
internal class ThreadDictionaryObj
{
public Task TaskObj { get; set; }
public CancellationTokenSource CancellationToken { get; set; }
}
}