using NCC.Dependency;
using System;
using System.Collections.Generic;
using System.Timers;
namespace NCC.TaskScheduler
{
///
/// 内置时间调度器
///
[SuppressSniffer]
public sealed class SpareTimer : Timer
{
///
/// 构造函数
///
///
internal SpareTimer(string workerName = default) : base()
{
WorkerName = workerName ?? Guid.NewGuid().ToString("N");
// 记录当前定时器
if (!SpareTime.WorkerRecords.TryAdd(WorkerName, new WorkerRecord
{
Timer = this
})) throw new InvalidOperationException($"The worker name `{WorkerName}` is exist.");
}
///
/// 构造函数
///
///
///
internal SpareTimer(double interval, string workerName = default) : base(interval)
{
WorkerName = workerName ?? Guid.NewGuid().ToString("N");
// 记录当前定时器
if (!SpareTime.WorkerRecords.TryAdd(WorkerName, new WorkerRecord
{
Interlocked = 0,
Tally = 0,
Timer = this
})) throw new InvalidOperationException($"The worker name `{WorkerName}` is exist.");
}
///
/// 当前任务名
///
public string WorkerName { get; private set; }
///
/// 任务类型
///
public SpareTimeTypes Type { get; internal set; }
///
/// 任务描述
///
public string Description { get; internal set; }
///
/// 任务状态
///
public SpareTimeStatus Status { get; internal set; }
///
/// 执行类型
///
public SpareTimeExecuteTypes ExecuteType { get; internal set; } = SpareTimeExecuteTypes.Parallel;
///
/// 异常信息
///
public Dictionary Exception { get; internal set; } = new Dictionary();
///
/// 任务执行计数
///
public long Tally { get; internal set; } = 0;
}
}