SpareTimer.cs
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using NCC.Dependency;
using System;
using System.Collections.Generic;
using System.Timers;
namespace NCC.TaskScheduler
{
/// <summary>
/// 内置时间调度器
/// </summary>
[SuppressSniffer]
public sealed class SpareTimer : Timer
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="workerName"></param>
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.");
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="interval"></param>
/// <param name="workerName"></param>
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.");
}
/// <summary>
/// 当前任务名
/// </summary>
public string WorkerName { get; private set; }
/// <summary>
/// 任务类型
/// </summary>
public SpareTimeTypes Type { get; internal set; }
/// <summary>
/// 任务描述
/// </summary>
public string Description { get; internal set; }
/// <summary>
/// 任务状态
/// </summary>
public SpareTimeStatus Status { get; internal set; }
/// <summary>
/// 执行类型
/// </summary>
public SpareTimeExecuteTypes ExecuteType { get; internal set; } = SpareTimeExecuteTypes.Parallel;
/// <summary>
/// 异常信息
/// </summary>
public Dictionary<long, Exception> Exception { get; internal set; } = new Dictionary<long, Exception>();
/// <summary>
/// 任务执行计数
/// </summary>
public long Tally { get; internal set; } = 0;
}
}