This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
CueSDKAutoUpdate.cs
97 lines (84 loc) · 3 KB
/
CueSDKAutoUpdate.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Threading;
using System.Threading.Tasks;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET
{
/// <summary>
/// Static entry point to work with the Corsair-SDK.
/// </summary>
public static partial class CueSDK
{
#region Properties & Fields
private static CancellationTokenSource _updateTokenSource;
private static CancellationToken _updateToken;
private static Task _updateTask;
/// <summary>
/// Gets or sets the update-frequency in seconds. (Calculate by using '1f / updates per second')
/// </summary>
public static float UpdateFrequency { get; set; } = 1f / 30f;
private static UpdateMode _updateMode = UpdateMode.Manual;
/// <summary>
/// Gets or sets the update-mode for the device.
/// </summary>
public static UpdateMode UpdateMode
{
get { return _updateMode; }
set
{
_updateMode = value;
CheckUpdateLoop();
}
}
#endregion
#region Methods
/// <summary>
/// Checks if automatic updates should occur and starts/stops the update-loop if needed.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the requested update-mode is not available.</exception>
private static async void CheckUpdateLoop()
{
bool shouldRun;
switch (UpdateMode)
{
case UpdateMode.Manual:
shouldRun = false;
break;
case UpdateMode.Continuous:
shouldRun = true;
break;
default:
throw new ArgumentOutOfRangeException();
}
if (shouldRun && _updateTask == null) // Start task
{
_updateTokenSource?.Dispose();
_updateTokenSource = new CancellationTokenSource();
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token));
}
else if (!shouldRun && _updateTask != null) // Stop task
{
_updateTokenSource.Cancel();
await _updateTask;
_updateTask.Dispose();
_updateTask = null;
}
}
private static void UpdateLoop()
{
while (!_updateToken.IsCancellationRequested)
{
long preUpdateTicks = DateTime.Now.Ticks;
foreach (ICueDevice device in InitializedDevices)
device.Update();
int sleep = (int)((UpdateFrequency * 1000f) - ((DateTime.Now.Ticks - preUpdateTicks) / 10000f));
if (sleep > 0)
Thread.Sleep(sleep);
}
}
#endregion
}
}