Files
Analog_System_Monitor/analog_system_monitor_dotnet/TrayApp.cs

75 lines
2.0 KiB
C#

#nullable enable
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()
{
telemetry = new Telemetry();
telemetry.Initialize();
trayIcon = new NotifyIcon()
{
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath),
Visible = true,
Text = "Telemetry Running (UDP)"
};
var menu = new ContextMenuStrip();
menu.Items.Add("Exit", null, OnExit);
trayIcon.ContextMenuStrip = menu;
// Main telemetry timer
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
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)
{
telemetry.Dispose();
trayIcon.Visible = false;
Application.Exit();
}
}