mh_frame_sps/APT.Infrastructure.Core/Thread/LDXThreadPool.cs
2026-04-07 13:47:52 +08:00

161 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace APT.Infrastructure.Core
{
public class APTThreadPool
{
private static List<APTThreadWork> _workQueues = new List<APTThreadWork>();
private static List<APTThreadWork> _waiAddWorkQueues = new List<APTThreadWork>();
private static List<APTThreadWork> _removeList = new List<APTThreadWork>();
private static bool _isStop = false;
private static Thread _mainThread = null;
private static object _mainThreadLockObj = new object();
private static object _addThreadLockObj = new object();
private static int MainThreadSleepTime = 10 * 1000;
public static void Start(bool isMainThreadByBackground=false)
{
DoStartThread(isMainThreadByBackground);
}
public static void Stop()
{
lock (_mainThreadLockObj)
{
_isStop = true;
}
if (_mainThread != null)
_mainThread.Abort();
}
public static void Stop(APTThreadWork work)
{
lock (_mainThreadLockObj)
{
_removeList.Add(work);
}
}
public static void AddWorkThread(APTThreadWork work)
{
AddWorkThread(DateTime.MinValue, work);
}
public static void AddWorkThread(DateTime runTime, APTThreadWork work)
{
if (runTime != DateTime.MinValue)
work.RunTime = runTime;
lock (_addThreadLockObj)
{
_waiAddWorkQueues.Add(work);
}
}
private static void DoStartThread(bool isBackground)
{
_mainThread = new Thread(DoThreadWork) { IsBackground = isBackground };
_mainThread.Start();
}
private static void DoThreadWork()
{
while (true)
{
try
{
lock (_mainThreadLockObj)
{
if (_removeList != null && _removeList.Count > 0)
{
foreach (var item in _removeList)
_workQueues.Remove(item);
_removeList.Clear();
}
}
lock (_addThreadLockObj)
{
if (_waiAddWorkQueues != null && _waiAddWorkQueues.Count > 0)
foreach (var item in _waiAddWorkQueues)
_workQueues.Add(item);
_waiAddWorkQueues.Clear();
}
if (_workQueues != null && _workQueues.Count > 0)
{
lock (_mainThreadLockObj)
{
DateTime nowTime = DateTime.Now.ToLocalTime();
foreach (APTThreadWork work in _workQueues)
{
if (work == null || work.ThreadState == ThreadState.Running || work.ThreadState == ThreadState.Stop) continue;
if (_isStop) break;
DateTime runTime = work.RunTime;
if (runTime == DateTime.MinValue)
{
work.ThreadState = ThreadState.Stop;
_removeList.Add(work);
continue;
}
if (runTime <= nowTime)
{
work.ThreadState = ThreadState.Running;
ThreadPool.QueueUserWorkItem(new WaitCallback(InternelExecuteWork), work);
}
}
if (_isStop) break;
}
}
}
catch
{
}
if (_workQueues == null || _workQueues.Count == 0)
Thread.Sleep(MainThreadSleepTime);
else
Thread.Sleep(3*1000);//休眠3秒钟
}
}
private static string _sss = string.Empty;
public static void InternelExecuteWork(object obj)
{
APTThreadWork work = obj as APTThreadWork;
if (work == null) return;
try
{
DateTime runTime = work.RunTime;
work.ThreadState = ThreadState.Running;
work.Execute();
if (work.NextRunTime(ref runTime))
{
work.ThreadState = ThreadState.Wait;
work.RunTime = runTime;
}
else
{
work.ThreadState = ThreadState.Stop;
lock (_mainThreadLockObj)
{
_removeList.Add(work);
}
}
}
catch
{
lock (_mainThreadLockObj)
{
_removeList.Add(work);
}
work.ThreadState = ThreadState.Wait;
}
}
}
}