From b91c526d2e7c742d75a8179382c3b057adaa4f22 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sun, 26 Feb 2023 17:56:01 +0800 Subject: [PATCH] [exe] Show server status --- extra/exe-builder/Program.cs | 28 ++++++++++++++++++-- extra/exe-builder/Properties/AssemblyInfo.cs | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/extra/exe-builder/Program.cs b/extra/exe-builder/Program.cs index 1385e8303..c120d6df5 100644 --- a/extra/exe-builder/Program.cs +++ b/extra/exe-builder/Program.cs @@ -5,6 +5,7 @@ using System.Drawing; using System.IO; using System.Linq; using System.Net; +using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using System.Threading.Tasks; @@ -33,19 +34,26 @@ namespace UptimeKuma { private NotifyIcon trayIcon; private Process process; + private MenuItem statusMenuItem; private MenuItem runWhenStarts; private RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); - public UptimeKumaApplicationContext() - { + + public UptimeKumaApplicationContext() { + var startingText = "Starting server..."; trayIcon = new NotifyIcon(); + trayIcon.Text = startingText; runWhenStarts = new MenuItem("Run when system starts", RunWhenStarts); runWhenStarts.Checked = registryKey.GetValue(appName) != null; + statusMenuItem = new MenuItem(startingText); + statusMenuItem.Enabled = false; + trayIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); trayIcon.ContextMenu = new ContextMenu(new MenuItem[] { + statusMenuItem, new("Open", Open), //new("Debug Console", DebugConsole), runWhenStarts, @@ -109,6 +117,22 @@ namespace UptimeKuma { process.Start(); //Open(null, null); + // Async task to check if the server is ready + Task.Run(() => { + var runningText = "Server is running"; + using TcpClient tcpClient = new TcpClient(); + while (true) { + try { + tcpClient.Connect("127.0.0.1", 3001); + statusMenuItem.Text = runningText; + trayIcon.Text = runningText; + break; + } catch (Exception) { + System.Threading.Thread.Sleep(2000); + } + } + }); + } catch (Exception e) { MessageBox.Show("Startup failed: " + e.Message, "Uptime Kuma Error"); } diff --git a/extra/exe-builder/Properties/AssemblyInfo.cs b/extra/exe-builder/Properties/AssemblyInfo.cs index 2552870b3..59b36bb81 100644 --- a/extra/exe-builder/Properties/AssemblyInfo.cs +++ b/extra/exe-builder/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Uptime Kuma")] -[assembly: AssemblyCopyright("Copyright © 2022 Louis Lam")] +[assembly: AssemblyCopyright("Copyright © 2023 Louis Lam")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")]