-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTimerHelper.cs
More file actions
82 lines (74 loc) · 2.2 KB
/
TimerHelper.cs
File metadata and controls
82 lines (74 loc) · 2.2 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 System;
using System.Timers;
namespace Helper.Core.Library
{
public class TimerHelper
{
#region 私有属性常量
private Timer timer;
private Action callback;
private int repeat;
private bool pauseStatus;
#endregion
#region 对外公开方法
/// <summary>
/// 任务运行,间隔时间
/// </summary>
/// <param name="callback">回调函数</param>
/// <param name="interval">执行间隔</param>
/// <param name="repeat">重复次数,小于 0 表示无限次</param>
/// <param name="executeCallback">是否立刻执行</param>
/// <returns></returns>
public void Run(Action callback, int interval = 1000, int repeat = 1, bool executeCallback = false)
{
this.callback = callback;
this.repeat = repeat;
this.pauseStatus = false;
if (this.timer != null) this.Stop();
this.timer = new Timer();
this.timer.Interval = interval;
this.timer.Elapsed += OnTimerElapsedHandler;
this.timer.Start();
if (executeCallback && callback != null) callback();
}
/// <summary>
/// 暂停
/// </summary>
public void Pause()
{
this.pauseStatus = true;
}
/// <summary>
/// 继续
/// </summary>
public void Continue()
{
this.pauseStatus = false;
}
/// <summary>
/// 停止任务
/// </summary>
public void Stop()
{
if (this.timer != null)
{
this.timer.Elapsed -= OnTimerElapsedHandler;
this.timer.Stop();
}
this.timer = null;
}
#endregion
#region 逻辑处理私有函数
private void OnTimerElapsedHandler(object sender, ElapsedEventArgs e)
{
if (this.pauseStatus) return;
if (this.repeat > 0) this.repeat--;
if (this.repeat == 0) this.Stop();
if (this.callback != null) this.callback();
}
#endregion
}
}