added exception handling, fixes errors when waking up, or missing network

This commit is contained in:
2026-01-18 14:48:32 +01:00
parent c1d7ba4b3d
commit b8d5e25352
3 changed files with 62 additions and 8 deletions

View File

@@ -145,7 +145,6 @@ public class Telemetry : IDisposable
float gpuTemp = GetGpuTemperaturePercent(); float gpuTemp = GetGpuTemperaturePercent();
float vram = GetGpuVramPercent(); float vram = GetGpuVramPercent();
// Prepare 8 floats (futureproof)
float[] packet = float[] packet =
{ {
cpu, cpu,
@@ -154,8 +153,8 @@ public class Telemetry : IDisposable
gpu3d, gpu3d,
gpuTemp, gpuTemp,
vram, vram,
0f, // reserved for future use 0f,
0f // reserved for future use 0f
}; };
udp.SendFloats(packet); udp.SendFloats(packet);

View File

@@ -3,11 +3,14 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Win32;
public class TrayApp : ApplicationContext public class TrayApp : ApplicationContext
{ {
private NotifyIcon trayIcon; private NotifyIcon trayIcon;
private Telemetry telemetry; private Telemetry telemetry;
private System.Windows.Forms.Timer timer;
private bool telemetryPaused = false;
public TrayApp() public TrayApp()
{ {
@@ -25,10 +28,41 @@ public class TrayApp : ApplicationContext
menu.Items.Add("Exit", null, OnExit); menu.Items.Add("Exit", null, OnExit);
trayIcon.ContextMenuStrip = menu; trayIcon.ContextMenuStrip = menu;
var timer = new System.Windows.Forms.Timer(); // Main telemetry timer
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000; timer.Interval = 1000;
timer.Tick += (s, e) => telemetry.UpdateAndSend(); timer.Tick += (s, e) =>
{
if (!telemetryPaused)
telemetry.UpdateAndSend();
};
timer.Start(); 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) private void OnExit(object? sender, EventArgs e)

View File

@@ -45,11 +45,32 @@ public class UdpSender : IDisposable
} }
public void SendFloats(float[] values) public void SendFloats(float[] values)
{
try
{ {
string packet = string.Join(",", values); string packet = string.Join(",", values);
byte[] data = System.Text.Encoding.ASCII.GetBytes(packet); byte[] data = System.Text.Encoding.ASCII.GetBytes(packet);
client.Send(data, data.Length, endpoint); 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() public void Dispose()
{ {