From b8d5e2535206243bc1fd806b82fdbc5092153f6c Mon Sep 17 00:00:00 2001 From: Robin Cerny Date: Sun, 18 Jan 2026 14:48:32 +0100 Subject: [PATCH] added exception handling, fixes errors when waking up, or missing network --- analog_system_monitor_dotnet/Telemetry.cs | 5 ++- analog_system_monitor_dotnet/TrayApp.cs | 38 +++++++++++++++++++++-- analog_system_monitor_dotnet/UdpSender.cs | 27 ++++++++++++++-- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/analog_system_monitor_dotnet/Telemetry.cs b/analog_system_monitor_dotnet/Telemetry.cs index 026ee34..8051c62 100644 --- a/analog_system_monitor_dotnet/Telemetry.cs +++ b/analog_system_monitor_dotnet/Telemetry.cs @@ -145,7 +145,6 @@ public class Telemetry : IDisposable float gpuTemp = GetGpuTemperaturePercent(); float vram = GetGpuVramPercent(); - // Prepare 8 floats (future‑proof) float[] packet = { cpu, @@ -154,8 +153,8 @@ public class Telemetry : IDisposable gpu3d, gpuTemp, vram, - 0f, // reserved for future use - 0f // reserved for future use + 0f, + 0f }; udp.SendFloats(packet); diff --git a/analog_system_monitor_dotnet/TrayApp.cs b/analog_system_monitor_dotnet/TrayApp.cs index 3582d83..0995cb0 100644 --- a/analog_system_monitor_dotnet/TrayApp.cs +++ b/analog_system_monitor_dotnet/TrayApp.cs @@ -3,11 +3,14 @@ using System; using System.Drawing; using System.Windows.Forms; +using Microsoft.Win32; public class TrayApp : ApplicationContext { private NotifyIcon trayIcon; private Telemetry telemetry; + private System.Windows.Forms.Timer timer; + private bool telemetryPaused = false; public TrayApp() { @@ -25,10 +28,41 @@ public class TrayApp : ApplicationContext menu.Items.Add("Exit", null, OnExit); trayIcon.ContextMenuStrip = menu; - var timer = new System.Windows.Forms.Timer(); + // Main telemetry timer + timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; - timer.Tick += (s, e) => telemetry.UpdateAndSend(); + timer.Tick += (s, e) => + { + if (!telemetryPaused) + telemetry.UpdateAndSend(); + }; timer.Start(); + + // Detect system sleep/wake + SystemEvents.PowerModeChanged += OnPowerModeChanged; + } + + private void OnPowerModeChanged(object? sender, PowerModeChangedEventArgs e) + { + if (e.Mode == PowerModes.Suspend) + { + telemetryPaused = true; + } + else if (e.Mode == PowerModes.Resume) + { + telemetryPaused = true; + + // Give Windows time to restore networking + var resumeTimer = new System.Windows.Forms.Timer(); + resumeTimer.Interval = 3000; // 3 seconds + resumeTimer.Tick += (s, ev) => + { + telemetryPaused = false; + resumeTimer.Stop(); + resumeTimer.Dispose(); + }; + resumeTimer.Start(); + } } private void OnExit(object? sender, EventArgs e) diff --git a/analog_system_monitor_dotnet/UdpSender.cs b/analog_system_monitor_dotnet/UdpSender.cs index 6779784..ac9a631 100644 --- a/analog_system_monitor_dotnet/UdpSender.cs +++ b/analog_system_monitor_dotnet/UdpSender.cs @@ -46,9 +46,30 @@ public class UdpSender : IDisposable public void SendFloats(float[] values) { - string packet = string.Join(",", values); - byte[] data = System.Text.Encoding.ASCII.GetBytes(packet); - client.Send(data, data.Length, endpoint); + try + { + string packet = string.Join(",", values); + byte[] data = System.Text.Encoding.ASCII.GetBytes(packet); + + client.Send(data, data.Length, endpoint); + } + catch (SocketException ex) when ( + ex.SocketErrorCode == SocketError.NetworkUnreachable || + ex.SocketErrorCode == SocketError.HostUnreachable || + ex.SocketErrorCode == SocketError.NetworkDown || + ex.SocketErrorCode == SocketError.AddressNotAvailable) + { + // Network not ready (sleep, reconnecting, etc.) + // Skip this tick silently. + } + catch (ObjectDisposedException) + { + // App is shutting down — ignore. + } + catch (Exception) + { + // Any other unexpected error — swallow to avoid crashing the tray app. + } } public void Dispose()