1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Fully implemented disposing on devices

This commit is contained in:
Darth Affe 2020-03-03 14:50:31 +01:00
parent 1cfdeefde1
commit 83edbc6eac
31 changed files with 174 additions and 33 deletions

View File

@ -157,8 +157,12 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() public virtual void Dispose()
{ {
SpecialDeviceParts.Clear(); try
LedMapping.Clear(); {
SpecialDeviceParts.Clear();
LedMapping.Clear();
}
catch { /* this really shouldn't happen */ }
} }
/// <summary> /// <summary>

View File

@ -172,7 +172,12 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
_sdk?.ReleaseControl(0); try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _sdk?.ReleaseControl(0); }
catch { /* at least we tried */ }
_sdk = null; _sdk = null;
} }

View File

@ -80,6 +80,15 @@ namespace RGB.NET.Devices.Asus
//} //}
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -268,6 +268,9 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _AsusSDK.UnloadAsusSDK(); } try { _AsusSDK.UnloadAsusSDK(); }
catch { /* at least we tried */ } catch { /* at least we tried */ }
} }

View File

@ -80,6 +80,9 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
if ((DeviceInfo is AsusRGBDeviceInfo deviceInfo) && (deviceInfo.Handle != IntPtr.Zero)) if ((DeviceInfo is AsusRGBDeviceInfo deviceInfo) && (deviceInfo.Handle != IntPtr.Zero))
Marshal.FreeHGlobal(deviceInfo.Handle); Marshal.FreeHGlobal(deviceInfo.Handle);

View File

@ -175,6 +175,9 @@ namespace RGB.NET.Devices.CoolerMaster
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
if (IsInitialized) if (IsInitialized)
foreach (IRGBDevice device in Devices) foreach (IRGBDevice device in Devices)
{ {

View File

@ -74,6 +74,9 @@ namespace RGB.NET.Devices.CoolerMaster
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
_CoolerMasterSDK.EnableLedControl(false, DeviceInfo.DeviceIndex); _CoolerMasterSDK.EnableLedControl(false, DeviceInfo.DeviceIndex);
base.Dispose(); base.Dispose();

View File

@ -293,6 +293,9 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _CUESDK.UnloadCUESDK(); } try { _CUESDK.UnloadCUESDK(); }
catch { /* at least we tried */ } catch { /* at least we tried */ }
} }

View File

@ -122,6 +122,15 @@ namespace RGB.NET.Devices.Corsair
Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ptr);
} }
/// <inheritdoc />
public override void Dispose()
{
try { DeviceUpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -116,7 +116,10 @@ namespace RGB.NET.Devices.DMX
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -59,6 +59,15 @@ namespace RGB.NET.Devices.DMX.E131
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => _updateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => _updateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { _updateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -70,6 +70,15 @@ namespace RGB.NET.Devices.Logitech
ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\Logitech", $"{layoutPath}.xml"), layout, true); ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\Logitech", $"{layoutPath}.xml"), layout, true);
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -184,6 +184,9 @@ namespace RGB.NET.Devices.Logitech
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _LogitechGSDK.LogiLedRestoreLighting(); } try { _LogitechGSDK.LogiLedRestoreLighting(); }
catch { /* at least we tried */ } catch { /* at least we tried */ }

View File

@ -68,6 +68,15 @@ namespace RGB.NET.Devices.Msi
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
=> DeviceUpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is int))); => DeviceUpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is int)));
/// <inheritdoc />
public override void Dispose()
{
try { DeviceUpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -164,6 +164,9 @@ namespace RGB.NET.Devices.Msi
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _MsiSDK.UnloadMsiSDK(); } try { _MsiSDK.UnloadMsiSDK(); }
catch { /* at least we tried */ } catch { /* at least we tried */ }
} }

View File

@ -78,8 +78,11 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
Reset(); Reset();
UpdateQueue.Dispose();
base.Dispose(); base.Dispose();
} }

View File

@ -122,7 +122,10 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -81,6 +81,15 @@ namespace RGB.NET.Devices.Razer
/// </summary> /// </summary>
public void Reset() => UpdateQueue.Reset(); public void Reset() => UpdateQueue.Reset();
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -224,6 +224,9 @@ namespace RGB.NET.Devices.Razer
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
TryUnInit(); TryUnInit();
try { _RazerSDK.UnloadRazerSDK(); } try { _RazerSDK.UnloadRazerSDK(); }

View File

@ -66,6 +66,9 @@ namespace RGB.NET.Devices.SoIP.Server
/// <inheritdoc /> /// <inheritdoc />
public override void Dispose() public override void Dispose()
{ {
try { _updateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose(); base.Dispose();
_tcpServer.Stop(); _tcpServer.Stop();

View File

@ -56,7 +56,7 @@ namespace RGB.NET.Devices.SoIP
{ {
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SoIPDeviceProvider)}"); if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SoIPDeviceProvider)}");
_instance = this; _instance = this;
UpdateTrigger = new DeviceUpdateTrigger(); UpdateTrigger = new DeviceUpdateTrigger();
} }
@ -130,8 +130,8 @@ namespace RGB.NET.Devices.SoIP
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
foreach (IRGBDevice device in Devices) try { UpdateTrigger?.Dispose(); }
device.Dispose(); catch { /* at least we tried */ }
} }
#endregion #endregion

View File

@ -83,6 +83,15 @@ namespace RGB.NET.Devices.SteelSeries
ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\SteelSeries", $"{layoutPath}.xml"), layout, true); ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\SteelSeries", $"{layoutPath}.xml"), layout, true);
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -117,11 +117,11 @@ namespace RGB.NET.Devices.SteelSeries
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try try { UpdateTrigger?.Dispose(); }
{ catch { /* at least we tried */ }
SteelSeriesSDK.Dispose();
} try { SteelSeriesSDK.Dispose(); }
catch {/* shit happens */} catch { /* shit happens */ }
} }
#endregion #endregion

View File

@ -73,6 +73,15 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -39,7 +39,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class. /// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class.
/// </summary> /// </summary>
/// <param name="portName">The name of the serial-port to connect to.</param> /// <param name="port">The name of the serial-port to connect to.</param>
public ArduinoWS281XDeviceDefinition(string port) public ArduinoWS281XDeviceDefinition(string port)
{ {
this.Port = port; this.Port = port;
@ -50,10 +50,8 @@ namespace RGB.NET.Devices.WS281X.Arduino
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<IRGBDevice> CreateDevices() public IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger)
{ {
DeviceUpdateTrigger updateTrigger = new DeviceUpdateTrigger();
ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, Port, BaudRate); ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
IEnumerable<(int channel, int ledCount)> channels = queue.GetChannels(); IEnumerable<(int channel, int ledCount)> channels = queue.GetChannels();
int counter = 0; int counter = 0;
@ -64,8 +62,6 @@ namespace RGB.NET.Devices.WS281X.Arduino
device.Initialize(ledCount); device.Initialize(ledCount);
yield return device; yield return device;
} }
updateTrigger.Start();
} }
#endregion #endregion

View File

@ -63,6 +63,15 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -43,7 +43,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class. /// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class.
/// </summary> /// </summary>
/// <param name="portName">The name of the serial-port to connect to.</param> /// <param name="port">The name of the serial-port to connect to.</param>
public BitwizardWS281XDeviceDefinition(string port) public BitwizardWS281XDeviceDefinition(string port)
{ {
this.Port = port; this.Port = port;
@ -54,17 +54,13 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<IRGBDevice> CreateDevices() public IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger)
{ {
DeviceUpdateTrigger updateTrigger = new DeviceUpdateTrigger();
BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, Port, BaudRate); BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
string name = Name ?? $"Bitwizard WS2812 USB ({Port})"; string name = Name ?? $"Bitwizard WS2812 USB ({Port})";
BitwizardWS2812USBDevice device = new BitwizardWS2812USBDevice(new BitwizardWS2812USBDeviceInfo(name), queue); BitwizardWS2812USBDevice device = new BitwizardWS2812USBDevice(new BitwizardWS2812USBDeviceInfo(name), queue);
device.Initialize(StripLength); device.Initialize(StripLength);
yield return device; yield return device;
updateTrigger.Start();
} }
#endregion #endregion

View File

@ -13,6 +13,6 @@ namespace RGB.NET.Devices.WS281X
/// Gets the devices defined by this definition. /// Gets the devices defined by this definition.
/// </summary> /// </summary>
/// <returns>The initialized devices defined by this definition.</returns> /// <returns>The initialized devices defined by this definition.</returns>
IEnumerable<IRGBDevice> CreateDevices(); IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger);
} }
} }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using RGB.NET.Core; using RGB.NET.Core;
namespace RGB.NET.Devices.WS281X namespace RGB.NET.Devices.WS281X
@ -37,6 +38,11 @@ namespace RGB.NET.Devices.WS281X
// ReSharper disable once ReturnTypeCanBeEnumerable.Global // ReSharper disable once ReturnTypeCanBeEnumerable.Global
public List<IWS281XDeviceDefinition> DeviceDefinitions { get; } = new List<IWS281XDeviceDefinition>(); public List<IWS281XDeviceDefinition> DeviceDefinitions { get; } = new List<IWS281XDeviceDefinition>();
/// <summary>
/// The <see cref="DeviceUpdateTrigger"/> used to trigger the updates for corsair devices.
/// </summary>
public DeviceUpdateTrigger UpdateTrigger { get; }
#endregion #endregion
#region Constructors #region Constructors
@ -49,6 +55,8 @@ namespace RGB.NET.Devices.WS281X
{ {
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}"); if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}");
_instance = this; _instance = this;
UpdateTrigger = new DeviceUpdateTrigger();
} }
#endregion #endregion
@ -69,17 +77,20 @@ namespace RGB.NET.Devices.WS281X
try try
{ {
UpdateTrigger?.Stop();
List<IRGBDevice> devices = new List<IRGBDevice>(); List<IRGBDevice> devices = new List<IRGBDevice>();
foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions) foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions)
{ {
try try
{ {
devices.AddRange(deviceDefinition.CreateDevices()); devices.AddRange(deviceDefinition.CreateDevices(UpdateTrigger));
} }
catch { if (throwExceptions) throw; } catch { if (throwExceptions) throw; }
} }
Devices = devices; UpdateTrigger?.Start();
Devices = new ReadOnlyCollection<IRGBDevice>(devices);
IsInitialized = true; IsInitialized = true;
} }
catch catch
@ -99,10 +110,10 @@ namespace RGB.NET.Devices.WS281X
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
if (IsInitialized) try { UpdateTrigger?.Dispose(); }
foreach (IRGBDevice device in Devices) catch { /* at least we tried */}
if (device is IDisposable disposable)
disposable.Dispose(); DeviceDefinitions.Clear();
} }
#endregion #endregion

View File

@ -63,6 +63,15 @@ namespace RGB.NET.Devices.Wooting.Generic
/// </summary> /// </summary>
protected abstract void InitializeLayout(); protected abstract void InitializeLayout();
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -143,6 +143,9 @@ namespace RGB.NET.Devices.Wooting
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _WootingSDK.Reset(); } try { _WootingSDK.Reset(); }
catch { /* Unlucky.. */ } catch { /* Unlucky.. */ }