mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2026-11-10 20:46:24 +00:00
Refactored auto-update to be SDK-wide
This commit is contained in:
1 parent
16c297611e
commit
8b87270b69
7 files changed
+139
-93
No files matched your search
@@ -29,35 +29,13 @@ namespace CUE.NET.Devices.Generic
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private CancellationTokenSource _updateTokenSource;
|
||||
private CancellationToken _updateToken;
|
||||
private Task _updateTask;
|
||||
private DateTime _lastUpdate = DateTime.Now;
|
||||
private static DateTime _lastUpdate = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// Gets generic information provided by CUE for the device.
|
||||
/// </summary>
|
||||
public IDeviceInfo DeviceInfo { get; }
|
||||
|
||||
private UpdateMode _updateMode = UpdateMode.Manual;
|
||||
/// <summary>
|
||||
/// Gets or sets the update-mode for the device.
|
||||
/// </summary>
|
||||
public UpdateMode UpdateMode
|
||||
{
|
||||
get { return _updateMode; }
|
||||
set
|
||||
{
|
||||
_updateMode = value;
|
||||
CheckUpdateLoop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the update-frequency in seconds. (Calculate by using '1f / updates per second')
|
||||
/// </summary>
|
||||
public float UpdateFrequency { get; set; } = 1f / 30f;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rectangle containing all LEDs of the device.
|
||||
/// </summary>
|
||||
@@ -166,9 +144,7 @@ namespace CUE.NET.Devices.Generic
|
||||
|
||||
// ReSharper disable once VirtualMemberCallInConstructor - I know, but I need this ...
|
||||
InitializeLeds();
|
||||
DeviceRectangle = RectangleHelper.CreateRectangleFromRectangles(((IEnumerable<CorsairLed>)this).Select(x => x.LedRectangle));
|
||||
|
||||
CheckUpdateLoop();
|
||||
DeviceRectangle = RectangleHelper.CreateRectangleFromRectangles((this).Select(x => x.LedRectangle));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -208,56 +184,6 @@ namespace CUE.NET.Devices.Generic
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update-Loop
|
||||
|
||||
/// <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>
|
||||
protected 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 void UpdateLoop()
|
||||
{
|
||||
while (!_updateToken.IsCancellationRequested)
|
||||
{
|
||||
long preUpdateTicks = DateTime.Now.Ticks;
|
||||
Update();
|
||||
int sleep = (int)((UpdateFrequency * 1000f) - ((DateTime.Now.Ticks - preUpdateTicks) / 10000f));
|
||||
if (sleep > 0)
|
||||
Thread.Sleep(sleep);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user