added exception handling, fixes errors when waking up, or missing network
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user