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

@@ -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)