wasSharp – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | |||
9 | namespace wasSharp.Timers |
||
10 | { |
||
11 | /////////////////////////////////////////////////////////////////////////// |
||
12 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
13 | /////////////////////////////////////////////////////////////////////////// |
||
14 | /// <summary> |
||
15 | /// Given a number of allowed events per seconds, this class allows you |
||
16 | /// to determine via the IsSafe property whether it is safe to trigger |
||
17 | /// another lined-up event. This is mostly used to check that throttles |
||
18 | /// are being respected. |
||
19 | /// </summary> |
||
20 | public class TimedThrottle : IDisposable |
||
21 | { |
||
22 | private readonly uint EventsAllowed; |
||
23 | private readonly object LockObject = new object(); |
||
24 | private Timer timer; |
||
25 | public uint TriggeredEvents; |
||
26 | |||
27 | public TimedThrottle(uint events, uint seconds) |
||
28 | { |
||
29 | EventsAllowed = events; |
||
30 | if (timer == null) |
||
31 | { |
||
20 | office | 32 | timer = new Timer(() => |
10 | office | 33 | { |
34 | lock (LockObject) |
||
35 | { |
||
36 | TriggeredEvents = 0; |
||
37 | } |
||
20 | office | 38 | }, seconds, seconds); |
10 | office | 39 | } |
40 | } |
||
41 | |||
42 | public bool IsSafe |
||
43 | { |
||
44 | get |
||
45 | { |
||
46 | lock (LockObject) |
||
47 | { |
||
48 | return ++TriggeredEvents <= EventsAllowed; |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public void Dispose() |
||
54 | { |
||
55 | Dispose(true); |
||
56 | GC.SuppressFinalize(this); |
||
57 | } |
||
58 | |||
59 | ~TimedThrottle() |
||
60 | { |
||
61 | Dispose(false); |
||
62 | } |
||
63 | |||
64 | private void Dispose(bool dispose) |
||
65 | { |
||
66 | if (timer != null) |
||
67 | { |
||
68 | timer.Dispose(); |
||
69 | timer = null; |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | } |