一个监视类,可以实现数据的分时更新功能
环境:.NET,C#
/// <summary>
/// 监控线程类
/// </summary>
public class WatchDogOpt
{
protected struct ____Task____
{
public long taskId;
public WatchFunc function;
public int delaySecond;
public int comeSecond;
public int remainTimes;
public ____Task____(WatchFunc function, int delaySecond, int effectTimes, int comeSecond)
{
this.taskId = DateTime.Now.Ticks;
this.function = function;
this.delaySecond = delaySecond;
this.remainTimes = effectTimes;
this.comeSecond = comeSecond;
}
};
protected struct ____Thread____
{
public Thread thread;
public long threadId;
public ____Thread____(Thread thread, long threadId)
{
this.thread = thread;
this.threadId = threadId;
}
}
static protected int objectNumber = 0;
private IList<____Task____> taskList;
private IList<____Thread____> threadList;
private int pastTime;
private int state;
/// <summary>
/// 当前已创建的实例数,只读
/// </summary>
public int ObjectNumber
{
get { return objectNumber; }
}
/// <summary>
/// 监视线程代理
/// </summary>
public delegate void WatchFunc(object isEnd);
/// <summary>
/// 构造函数
/// </summary>
public WatchDogOpt()
{
taskList = new List<____Task____>();
threadList = new List<____Thread____>();
pastTime = 0;
state = -1;
objectNumber++;
}
/// <summary>
/// 查询任务是否完成
/// </summary>
public bool CheckTask(long taskId)
{
foreach (____Thread____ threadmsg in threadList)
{
if (threadmsg.threadId == taskId && threadmsg.thread.ThreadState != ThreadState.Stopped)
return false;
}
return true;
}
/// <summary>
/// 清理任务
/// </summary>
public bool ClearTask(long taskId)
{
foreach (____Thread____ threadmsg in threadList)
{
if (threadmsg.threadId == taskId && threadmsg.thread.ThreadState != ThreadState.Stopped)
threadmsg.thread.Abort();
}
return true;
}
/// <summary>
/// 添加需要处理的任务
/// 返回值为任务编号,此编号将挂接到所有任务上
/// </summary>
public long TaskAdd(WatchFunc taskFunc, int delaySecond, int effectTimes)
{
taskList.Add(new ____Task____(taskFunc, delaySecond, effectTimes, pastTime));
return taskList[taskList.Count - 1].taskId;
}
/// <summary>
/// 删除需要处理的任务
/// </summary>
public bool TaskDel(long taskId)
{
foreach (____Task____ task in taskList)
{
if (task.taskId == taskId)
taskList.Remove(task);
}
return true;
}
/// <summary>
/// 开始监视
/// </summary>
public void Start()
{
Thread thread = new Thread(new ThreadStart(Work));
thread.Priority = System.Threading.ThreadPriority.BelowNormal;
state = 1;
thread.Start();
}
/// <summary>
/// 结束监视
/// </summary>
public void Stop()
{
if (state != -1)
state = 2;
}
/// <summary>
/// 暂停监视
/// </summary>
public void Suspend()
{
if (state != -1)
state = 0;
}
/// <summary>
/// 恢复监视
/// </summary>
public void Continue()
{
if (state != -1)
state = 1;
}
private void Work()
{
while (true)
{
Thread.Sleep(1000);
if (state == 0)
continue;
if (state != 1)
break;
if ((++pastTime % 2) == 0)
ReFleshAllThread();
for (int i = 0; i < taskList.Count; i++)
{
if ((pastTime - taskList.comeSecond) % taskList.delaySecond == 0)
{
Thread thread = new Thread(new ParameterizedThreadStart(taskList.function));
thread.Priority = System.Threading.ThreadPriority.AboveNormal;
if (taskList.remainTimes == int.MinValue || taskList.remainTimes > 0)
{
thread.Start(false);
____Task____ task = taskList;
task.remainTimes--;
taskList.RemoveAt(i);
taskList.Insert(i, task);
}
else
{
thread.Start(true);
taskList.RemoveAt(i--);
}
threadList.Add(new ____Thread____(thread, taskList.taskId));
}
}
}
}
private void ReFleshAllThread()
{
for (int i = 0; i < threadList.Count;)
{
if (threadList.thread.ThreadState == ThreadState.Stopped)
threadList.RemoveAt(i);
else
i++;
}
}
}
监视线程
最新推荐文章于 2022-05-18 17:37:11 发布