using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Store.Framework.ThreadManage
{
///
/// 线程管理
///
public class ThreadManager
{
//private int _sleep;
private Thread _listen;
private List _actions = new List();
private CancellationTokenSource cancellationToken = new CancellationTokenSource();
private int _countThreads = 10;
///
/// 间隔秒数
///
public int Interval { get; set; } = 1;
///
/// 线程数
///
public int CountThreads
{
get => _countThreads;
set
{
_countThreads = value;
ThreadPool.SetMinThreads(_countThreads, _countThreads);
ThreadPool.SetMaxThreads(_countThreads, _countThreads);
}
}
///
/// 是否完成状态
///
public bool Completed { get; private set; }
///
/// 正在运行的线程数量
///
public int CountActiveThreads
{
get
{
return -1;
}
}
///
/// 实例 默认线程10 间隔 1秒
///
public ThreadManager()
{
}
///
///
///
/// 线程数
public ThreadManager(int countThreads)
{
CountThreads = countThreads;
}
///
///
///
/// 线程数
/// 间隔时间
public ThreadManager(int countThreads, int interval)
{
CountThreads = countThreads;
Interval = interval;
}
private void Listen()
{
Completed = false;
cancellationToken = new CancellationTokenSource();
_listen = new Thread(() =>
{
while (!cancellationToken.IsCancellationRequested && _actions.Count > 0)
{
DateTime time = DateTime.Now;
int countCompeleted = 0;
for (int i = 0; i < _actions.Count; i++)
{
var action = _actions[i];
if (action.Completed) countCompeleted++;
if (!action.Completed && !action.IsWork && CountThreads > CountActiveThreads)
//if (((time >= action.EndTime && action.Loop) || (!action.Completed && !action.Loop)) && !action.IsWork && CountThreads > CountActiveThreads)
{
action.IsWork = true;
ThreadPool.QueueUserWorkItem(o =>
{
WorkUnit unit = (WorkUnit)o;
unit.Action.Invoke(unit);
unit.IsWork = false;
unit.Completed = true; //!unit.Loop;
}, action);
}
//int subTime = (int)(action.EndTime - time).TotalMilliseconds;
//if (subTime < _sleep)
//{
// if (subTime < 0)
// {
// _sleep = 0;
// }
// else
// {
// _sleep = subTime;
// }
//}
}
Completed = countCompeleted == _actions.Count;
//Thread.Sleep(_sleep);
//_sleep = int.MaxValue;
Thread.Sleep(1000 * Interval);
}
});
_listen.IsBackground = true;
_listen.Start();
}
///
/// Clear all tasks.
///
public void Clear() => _actions.Clear();
///
/// Start Thread Manager.
///
public async void Start()
{
await Task.Run(() =>
{
//Console.WriteLine("123");
Listen();
}
);
}
///
/// Stop Thread Manager.
///
public void Stop()
{
Completed = true;
if (cancellationToken != null && !cancellationToken.IsCancellationRequested)
{
cancellationToken.Cancel();
cancellationToken.Dispose();
}
if (_listen != null)
{
_listen.Abort();
}
}
///
/// Waiting for work to finish
///
public void Wait()
{
while (!Completed) { Thread.Sleep(100); }
}
///
/// 添加任务
///
/// 委托任务
/// Repeat the task.
public void AddTask(WorkAction action, object arg = null)
{
WorkUnit unit = new WorkUnit();
unit.Action = action;
unit.Completed = false;
unit.Argument = arg;
_actions.Add(unit);
}
public void AddTask(Action action)
{
WorkUnit unit = new WorkUnit();
unit.Action = a => action.Invoke();
unit.Completed = false;
_actions.Add(unit);
}
}
}