Files
KJD/Store.Framework/ThreadManage/ThreadManager.cs
T
2026-09-11 19:11:12 +08:00

188 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Store.Framework.ThreadManage
{
/// <summary>
/// 线程管理
/// </summary>
public class ThreadManager
{
//private int _sleep;
private Thread _listen;
private List<WorkUnit> _actions = new List<WorkUnit>();
private CancellationTokenSource cancellationToken = new CancellationTokenSource();
private int _countThreads = 10;
/// <summary>
/// 间隔秒数
/// </summary>
public int Interval { get; set; } = 1;
/// <summary>
/// 线程数
/// </summary>
public int CountThreads
{
get => _countThreads;
set
{
_countThreads = value;
ThreadPool.SetMinThreads(_countThreads, _countThreads);
ThreadPool.SetMaxThreads(_countThreads, _countThreads);
}
}
/// <summary>
/// 是否完成状态
/// </summary>
public bool Completed { get; private set; }
/// <summary>
/// 正在运行的线程数量
/// </summary>
public int CountActiveThreads
{
get
{
return -1;
}
}
/// <summary>
/// 实例 默认线程10 间隔 1秒
/// </summary>
public ThreadManager()
{
}
/// <summary>
///
/// </summary>
/// <param name="countThreads">线程数</param>
public ThreadManager(int countThreads)
{
CountThreads = countThreads;
}
/// <summary>
///
/// </summary>
/// <param name="countThreads">线程数</param>
/// <param name="frequency">间隔时间</param>
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();
}
/// <summary>
/// Clear all tasks.
/// </summary>
public void Clear() => _actions.Clear();
/// <summary>
/// Start Thread Manager.
/// </summary>
public async void Start()
{
await Task.Run(() =>
{
//Console.WriteLine("123");
Listen();
}
);
}
/// <summary>
/// Stop Thread Manager.
/// </summary>
public void Stop()
{
Completed = true;
if (cancellationToken != null && !cancellationToken.IsCancellationRequested)
{
cancellationToken.Cancel();
cancellationToken.Dispose();
}
if (_listen != null)
{
_listen.Abort();
}
}
/// <summary>
/// Waiting for work to finish
/// </summary>
public void Wait()
{
while (!Completed) { Thread.Sleep(100); }
}
/// <summary>
/// 添加任务
/// </summary>
/// <param name="action">委托任务</param>
/// <param name="loop">Repeat the task.</param>
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);
}
}
}