using System;
using System.Collections.Generic;
using MrWu.Debug;
using System.Threading;
namespace Server
{
///
/// 定时器标记管理
///
public class TimerManager
{
///
/// 是否触发定时器事件
///
private volatile bool isTimerMillEvt;
///
/// 秒级定时器
///
private volatile bool isSecondTimeEvt;
private volatile int mInterval;
///
/// 是否在运行中
///
public bool isRun {
get;
private set;
}
///
/// 触发定时器的时间间隔
///
public int Interval {
get {
return mInterval;
}
}
///
/// 设置定时器的时间间隔
///
///
public void SetInterval(int Interval) {
if (Interval < 1) {
Debug.Error("不能设置这么小的事件间隔:" + Interval);
return;
}
mInterval = Interval;
}
///
/// 构造
///
/// 定时器的时间间隔
public TimerManager(int Interval) {
if (Interval < 1)
throw new ArgumentOutOfRangeException("Interval 必须为大于 1 的值!");
this.mInterval = Interval;
}
///
/// 秒级定时器
///
private Timer SecondTimer;
///
/// 毫秒级定时器
///
private Timer MillTimer;
///
/// 开始定时器的触发
///
public void Start() {
if (SecondTimer == null) {
SecondTimer = new Timer(SecoendTime, null, 0, 1000);
MillTimer = new Timer(MillTime, null, 0, Interval);
} else {
SecondTimer.Change(0, 1000);
MillTimer.Change(0, 100);
}
}
///
/// 停止定时器的触发
///
public void Stop() {
SecondTimer.Change(-1, 1000);
MillTimer.Change(-1, Interval);
}
private void SecoendTime(object state) {
isSecondTimeEvt = true;
}
private void MillTime(object state) {
isTimerMillEvt = true;
}
///
/// 获取是否需要定时器触发
///
///
public bool GetSecoend() {
if (isSecondTimeEvt) {
isSecondTimeEvt = false;
return true;
}
return false;
}
///
/// 获取是否需要触发毫秒定时器
///
///
public bool GetMillScoend() {
if (isTimerMillEvt) {
isTimerMillEvt = false;
return true;
}
return false;
}
}
}