From ef25ba94b9cdedefcda1e8b4317020f5c04aba97 Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 6 Aug 2025 16:15:33 +0800 Subject: [PATCH] =?UTF-8?q?Tray=EF=BC=9A=E5=A2=9E=E5=8A=A0=20Instance=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=EF=BC=8C=E4=BD=9C=E4=B8=BA=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Windows/WinForm/Tray.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Apewer.Windows/WinForm/Tray.cs b/Apewer.Windows/WinForm/Tray.cs index 4a303af..436e3fd 100644 --- a/Apewer.Windows/WinForm/Tray.cs +++ b/Apewer.Windows/WinForm/Tray.cs @@ -311,16 +311,22 @@ namespace Apewer.WinForm #region run + /// 唯一实例。 + public static Tray Instance { get; private set; } + /// 已启动的服务名称。 public static string ServiceName { get; private set; } - /// 在当前线程运行托盘程序,并启动消息循环。 - /// 启动托盘后执行的程序。 + /// 在当前线程运行托盘程序,并启动消息循环。此方法应在主线程中调用,并且此方法将阻塞当前线程。 + /// 托盘启动后,将在后台线程执行参数中指定的程序。 + /// (在后台线程执行)启动托盘后执行的程序。 /// + /// [STAThread] public static void Run(Action action) { if (action == null) throw new ArgumentNullException(nameof(action)); + if (Instance != null) throw new InvalidOperationException($"已存在实例,无法再次启动。"); Control.CheckForIllegalCrossThreadCalls = false; Application.EnableVisualStyles(); @@ -337,14 +343,14 @@ namespace Apewer.WinForm } } - var instance = new Tray(action); + Instance = new Tray(action); // action.Invoke(instance); Application.Run(); } /// 启动服务。 - /// 服务启动后执行的程序。 - /// 停止服务时执行的程序。 + /// (在后台线程执行)服务启动后执行的程序。 + /// (同步执行)停止服务时执行的程序。 public static void Service(Action onStart, Action onStop = null) { var processName = Process.GetCurrentProcess().ProcessName;