Timer定時器的設(shè)計方法
/**////
/// 委托
///
private TimerCallback timerDelegate;
/**////
/// 靜態(tài)實(shí)例
///
private static readonly PaperManager self = new PaperManager();
/**////
/// 構(gòu)造函數(shù)
///
public PaperManager()
{
timerDelegate = new TimerCallback(CheckStatus);
}
/**////
///
///
///
public static PaperManager getInstance()
{
return self;
}
/**////
/// 設(shè)置啟動時間間隔
///
///天
///小時
///分鐘
///秒
///毫秒
public void setDueTime(int days, int hours, int minutes, int seconds, int milisecond)
{
dueTime = new TimeSpan(days, hours, minutes, seconds, milisecond);
}
/**////
/// 設(shè)置回調(diào)時間間隔
///
///天
///小時
///分鐘
///秒
///毫秒
public void setPeriod(int days, int hours, int minutes, int seconds, int milisecond)
{
period = new TimeSpan(days, hours, minutes, seconds, milisecond);
}
/**////
/// 開始
///
public void Start()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
dueTime = TimeSpan.FromSeconds(0);
period = TimeSpan.FromSeconds(10);
iTimer = new Timer(timerDelegate, autoEvent, dueTime, period);
autoEvent.WaitOne(5000, false);
iTimer.Change(dueTime, period);
}
/**////
/// 停止
///
public void Stop()
{
iTimer.Dispose();
}
/**////
/// 執(zhí)行一次
///
public void ExcuteOneTime()
{
if (iTimer != null)
{
iTimer.Dispose();
}
//如果 period 為零 (0) 或 -1 毫秒,而且 dueTime 為正,則只會調(diào)用一次 callback;
//計時器的定期行為將被禁用,但通過使用 Change 方法可以重新啟用該行為。
setDueTime(0, 0, 0, 0, 1);
setPeriod(0, 0, 0, 0, -1);
AutoResetEvent autoEvent = new AutoResetEvent(false);
iTimer = new Timer(timerDelegate, autoEvent, dueTime, period);
autoEvent.WaitOne(5000, false);
iTimer.Change(dueTime, period);
}
/**////
/// 行為
///
///
public void CheckStatus(object nObject)
{
AutoResetEvent autoEvent = (AutoResetEvent)nObject;
評論