old_flsystem/类库/Api.Framework/SDK/TimerTask.cs

137 lines
4.3 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Api.Framework.SDK
{
/// <summary>
/// 定时器
/// </summary>
public abstract class TimerTask : IDisposable
{
/// <summary>
/// 是否在运行
/// </summary>
public new bool IsRunning { get; protected set; }
/// <summary>
/// 运行的数量
/// </summary>
public new long RunningCount { get; set; }
/// <summary>
/// 执行任务
/// </summary>
public new void ExecutionTask(object state, bool timedOut)
{
if (IsRunning) return;
try
{
IsRunning = true;
RunningCount++;
this.Run(state, timedOut);
}
catch (Exception ex)
{
EventClient.OnEvent(this, $"ERROR.:{this.GetType()}-{System.Reflection.MethodBase.GetCurrentMethod().Name}-->{ex.Message},{ex.StackTrace}");
}
finally
{
IsRunning = false;
}
}
/// <summary>
/// 运行主体 - 需要重写
/// </summary>
public abstract void Run(object state, bool timedOut);
/// <summary>
/// 销毁方法 - 可以被重写
/// </summary>
public new virtual void Dispose() { this.IsRunning = false; }
/// <summary>
/// RegisterdHandler
/// </summary>
public new RegisteredWaitHandle RegisterdHandler { get; private set; }
/// <summary>
/// WaitHandler
/// </summary>
public new WaitHandle WaitHandler { get; private set; }
static TimerTask() { Timers = new Dictionary<Type, TimerTask>(); }
private static Dictionary<Type, TimerTask> Timers { get; set; }
/// <summary>
/// 获得定时器
/// </summary>
public static TimerTask GetTimer<T>()
{
var type = typeof(T);
if (Timers.ContainsKey(type)) return Timers[type];
return null;
}
public static T NewTimer<T>(T task, int second, object state = null)
{
var _t = task as TimerTask;
if (_t == null) throw new Exception($"不支持{typeof(T)}类型,请继承[Api.Framework.TimerClient]!");
Close<T>();
var type = task.GetType();
_t.WaitHandler = new AutoResetEvent(false);
_t.RegisterdHandler = ThreadPool.RegisterWaitForSingleObject(_t.WaitHandler, new WaitOrTimerCallback(_t.ExecutionTask), state, second * 1000, false);
Timers[type] = _t;
return task;
}
/// <summary>
/// 创建一个定时器
/// </summary>
/// <returns></returns>
public static T NewTimer<T>(int second, object state = null)
{
if (second == 0) second = 1;
var time = System.Activator.CreateInstance<T>();
var _t = time as TimerTask;
if (_t == null) throw new Exception($"不支持{typeof(T)}类型,请继承[Api.Framework.TimerClient]!");
Close<T>();
var type = time.GetType();
_t.WaitHandler = new AutoResetEvent(false);
_t.RegisterdHandler = ThreadPool.RegisterWaitForSingleObject(_t.WaitHandler, new WaitOrTimerCallback(_t.ExecutionTask), state, second * 1000, false);
Timers[type] = _t;
return time;
}
/// <summary>
/// 关闭一个定时器
/// </summary>
public static new void Close<T>()
{
var type = typeof(T);
if (Timers.ContainsKey(type))
{
var t = Timers[type];
Timers.Remove(type);
if (t != null && t.WaitHandler != null && t.RegisterdHandler != null)
{
t.RegisterdHandler.Unregister(t.WaitHandler);
t.Dispose();
}
}
}
protected bool IsStart
{
get
{
if (this.WaitHandler != null && this.RegisterdHandler != null) return true;
return false;
}
}
}
}