diff --git a/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs b/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs index da4b3bf..5bf4a01 100644 --- a/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs +++ b/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs @@ -28,13 +28,16 @@ namespace RGB.NET.Devices.Wooting.Generic /// protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach ((object key, Color color) in dataSet) + lock (_WootingSDK.SdkLock) { - (int row, int column) = ((int, int))key; - _WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB()); - } + foreach ((object key, Color color) in dataSet) + { + (int row, int column) = ((int, int))key; + _WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB()); + } - _WootingSDK.ArrayUpdateKeyboard(); + _WootingSDK.ArrayUpdateKeyboard(); + } } #endregion diff --git a/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs b/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs index 88d39b4..15e3468 100644 --- a/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs +++ b/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs @@ -16,6 +16,7 @@ namespace RGB.NET.Devices.Wooting.Native #region Library management private static IntPtr _dllHandle = IntPtr.Zero; + internal static object SdkLock = new(); /// /// Reloads the SDK. @@ -52,6 +53,16 @@ namespace RGB.NET.Devices.Wooting.Native { if (_dllHandle == IntPtr.Zero) return; + Reset(); + Close(); + + _getDeviceInfoPointer = null; + _keyboardConnectedPointer = null; + _arrayUpdateKeyboardPointer = null; + _arraySetSinglePointer = null; + _resetPointer = null; + _closePointer = null; + // ReSharper disable once EmptyEmbeddedStatement - DarthAffe 20.02.2016: We might need to reduce the internal reference counter more than once to set the library free while (FreeLibrary(_dllHandle)) ; _dllHandle = IntPtr.Zero; @@ -116,3 +127,4 @@ namespace RGB.NET.Devices.Wooting.Native #endregion } } + \ No newline at end of file diff --git a/RGB.NET.Devices.Wooting/WootingDeviceProvider.cs b/RGB.NET.Devices.Wooting/WootingDeviceProvider.cs index 6decd4a..8d88fd1 100644 --- a/RGB.NET.Devices.Wooting/WootingDeviceProvider.cs +++ b/RGB.NET.Devices.Wooting/WootingDeviceProvider.cs @@ -55,25 +55,31 @@ namespace RGB.NET.Devices.Wooting protected override void InitializeSDK() { - _WootingSDK.Reload(); + lock (_WootingSDK.SdkLock) + { + _WootingSDK.Reload(); + } } protected override IEnumerable LoadDevices() { - if (_WootingSDK.KeyboardConnected()) + lock (_WootingSDK.SdkLock) { - _WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!; - IWootingRGBDevice? device = nativeDeviceInfo.Model switch + if (_WootingSDK.KeyboardConnected()) { - "Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()), - "Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()), - _ => null - }; + _WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!; + IWootingRGBDevice? device = nativeDeviceInfo.Model switch + { + "Wooting two" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo), GetUpdateTrigger()), + "Wooting one" => new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne), GetUpdateTrigger()), + _ => null + }; - if (device == null) - Throw(new RGBDeviceException("No supported Wooting keyboard connected")); - else - yield return device; + if (device == null) + Throw(new RGBDeviceException("No supported Wooting keyboard connected")); + else + yield return device; + } } } @@ -82,11 +88,11 @@ namespace RGB.NET.Devices.Wooting { base.Dispose(); - try { _WootingSDK.Close(); } - catch { /* Unlucky.. */ } - - try { _WootingSDK.UnloadWootingSDK(); } - catch { /* at least we tried */ } + lock (_WootingSDK.SdkLock) + { + try { _WootingSDK.UnloadWootingSDK(); } + catch { /* at least we tried */ } + } } #endregion