-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerUnitTest.cs
47 lines (39 loc) · 945 Bytes
/
TimerUnitTest.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
class ClassWithTimer: IDisposable
{
private readonly Timer _timer;
public ClassWithTimer()
{
_timer = new Timer(TimeSpan.FromMinutes(5).Milliseconds) {AutoReset = true};
_timer.Elapsed += TimerOnElapsed;
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
Value = CalculateNewValue(Value);
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
public int Value { get; private set; }
private static int CalculateNewValue(int oldValue)
{
int newValue = 0;
// ... calculation omitted ...
return newValue;
}
public void Dispose()
{
_timer?.Dispose();
}
}