You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace KGIS.Framework.ThreadManager
|
|
|
|
|
{
|
|
|
|
|
public class ThreadManager
|
|
|
|
|
{
|
|
|
|
|
public static ThreadManager Instance { get; } = new ThreadManager();
|
|
|
|
|
private List<CustomThread<object>> Threads = new List<CustomThread<object>>();
|
|
|
|
|
private int _ActiveCount;
|
|
|
|
|
public int ActiveCount
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Threads.FindAll(f => !((f.CurrThread.ThreadState & ThreadState.Stopped) == ThreadState.Stopped)).Count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private int _MaxThreadNum = -1;
|
|
|
|
|
public int MaxThreadNum
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_MaxThreadNum == -1)
|
|
|
|
|
{
|
|
|
|
|
_MaxThreadNum = Environment.ProcessorCount;
|
|
|
|
|
}
|
|
|
|
|
return _MaxThreadNum;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_MaxThreadNum = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ThreadManager()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
|
///静态方法,开启或唤醒一个线程去执行指定的回调方法
|
|
|
|
|
///</summary>
|
|
|
|
|
///<param name="waitCallback">委托实例</param>
|
|
|
|
|
///<param name="obj">传递给回调方法的参数</param>
|
|
|
|
|
///<param name="timeOut">当没有可用的线程时的等待时间,以毫秒为单位</param>
|
|
|
|
|
///<returns></returns>
|
|
|
|
|
public CustomThread<T> QueueUserWorkItem<T>(TaskCallback<T> waitCallback, T obj, TaskCallback<T> completeCallback = null, int timeOut = 10000)
|
|
|
|
|
{
|
|
|
|
|
//锁住共享资源,实现线程安全
|
|
|
|
|
lock (Threads)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var t = new CustomThread<T>();
|
|
|
|
|
t.Start<T>(waitCallback, obj, completeCallback);
|
|
|
|
|
//Threads.Add(t);
|
|
|
|
|
return t;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
//Monitor.Exit(Threads);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void InitThreadList()
|
|
|
|
|
{
|
|
|
|
|
//Threads = new ArrayList();
|
|
|
|
|
//for (int i = 0; i < 1; i++)
|
|
|
|
|
//{
|
|
|
|
|
// CustomThread t = new CustomThread();
|
|
|
|
|
// Threads.Add(t);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|