📄 scavengertimer.cs
字号:
//===============================================================================
// CSDN HeyCache
//===============================================================================
// 修改记录:[按最后修改时间倒排序]
// 2007.06.11 by tangwei
//
// 代码来源:参考于dotnet企业库3.0版
//===============================================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace HeyCacher.Components.Scavenger
{
/// <summary>
/// 用于资源回收的定时器
/// </summary>
public sealed class ScavengerTimer : IDisposable
{
private System.Timers.Timer Timer;
private System.Threading.Timer ThreadingTimer;
/// <summary>
/// 启动定时器
/// </summary>
/// <param name="callbackMethod">定时执行的回调,System.Timers.ElapsedEventHandler 或 System.Threading.TimerCallback</param>
/// <param name="CycleInSeconds">间隔时间</param>
public void StartPolling(object callbackMethod, int CycleInSeconds)
{
//参数检查
if (callbackMethod == null)
{
throw new ArgumentNullException("callbackMethod");
}
if (CycleInSeconds <= 0)
{
throw new ArgumentException("InvalidExpirationPollCycleTime", "pollCycleInMilliseconds");
}
//时间转换
int CycleInMilliseconds = CycleInSeconds * 1000;
//建立并启动定时器
if (callbackMethod is System.Timers.ElapsedEventHandler)
{
Timer = new System.Timers.Timer(CycleInMilliseconds);
Timer.Enabled = true;
Timer.Start();
Timer.Elapsed += (System.Timers.ElapsedEventHandler)callbackMethod;
}
else if (callbackMethod is System.Threading.TimerCallback)
{
ThreadingTimer = new System.Threading.Timer((System.Threading.TimerCallback)callbackMethod, null, CycleInMilliseconds, CycleInMilliseconds);
}
}
/// <summary>
///
/// </summary>
public void StopPolling()
{
if (Timer != null)
{
Timer.Stop();
Timer.Dispose();
Timer = null;
}
if (ThreadingTimer != null)
{
ThreadingTimer.Dispose();
ThreadingTimer = null;
}
}
/// <summary>
///
/// </summary>
void IDisposable.Dispose()
{
StopPolling();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -