From 3f8083be50b2d32fcab703047c300141af6d5ce7 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Mon, 12 Feb 2024 18:24:11 +0100 Subject: [PATCH] Exception handling for case when USB device refuses to open https://github.com/seerge/g-helper/issues/2057 --- app/Input/KeyboardListener.cs | 2 +- app/USB/AsusHid.cs | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/Input/KeyboardListener.cs b/app/Input/KeyboardListener.cs index 0b6ce49e..bbcd05e8 100644 --- a/app/Input/KeyboardListener.cs +++ b/app/Input/KeyboardListener.cs @@ -66,7 +66,7 @@ namespace GHelper.Input } catch (Exception ex) { - Logger.WriteLine(ex.ToString()); + Logger.WriteLine($"Listener exited: {ex.Message}"); } } diff --git a/app/USB/AsusHid.cs b/app/USB/AsusHid.cs index 7aac7f6c..19112311 100644 --- a/app/USB/AsusHid.cs +++ b/app/USB/AsusHid.cs @@ -90,18 +90,24 @@ public static class AsusHid if (devices is null) return; foreach (var device in devices) - using (var stream = device.Open()) - foreach (var data in dataList) - try - { - stream.Write(data); - Logger.WriteLine($"{log} {device.ProductID.ToString("X")}: {BitConverter.ToString(data)}"); - } - catch (Exception ex) - { - Logger.WriteLine($"Error writing {log} {device.ProductID.ToString("X")}: {ex.Message} {BitConverter.ToString(data)} "); - } - + try + { + using (var stream = device.Open()) + foreach (var data in dataList) + try + { + stream.Write(data); + Logger.WriteLine($"{log} {device.ProductID.ToString("X")}: {BitConverter.ToString(data)}"); + } + catch (Exception ex) + { + Logger.WriteLine($"Error writing {log} {device.ProductID.ToString("X")}: {ex.Message} {BitConverter.ToString(data)} "); + } + } + catch (Exception ex) + { + Logger.WriteLine($"Error opening {log} {device.ProductID.ToString("X")}: {ex.Message}"); + } } public static void WriteAura(byte[] data)