From 8db082465935808d588d4101c5f80f7e8a2eeeb7 Mon Sep 17 00:00:00 2001 From: muqing <1966944300@qq.com> Date: Fri, 19 Jul 2024 15:58:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=A4=8D=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RustTools/App.xaml.cs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/RustTools/App.xaml.cs b/RustTools/App.xaml.cs index 20135b9..95beea7 100644 --- a/RustTools/App.xaml.cs +++ b/RustTools/App.xaml.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.UI.Xaml; @@ -11,6 +12,7 @@ using RustTools.Models; using RustTools.Services; using RustTools.ViewModels; using RustTools.Views; +using System.Threading; namespace RustTools; @@ -45,6 +47,11 @@ public partial class App : Application public App() { InitializeComponent(); + System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("RustTools");//获取指定的进程名 + if (myProcesses.Length > 1) //如果可以获取到知道的进程名则说明已经启动 + { + Exit(); //关闭系统 + } Host = Microsoft.Extensions.Hosting.Host. CreateDefaultBuilder(). @@ -104,4 +111,27 @@ public partial class App : Application await App.GetService().ActivateAsync(args); } + + private Mutex mutex; + private void InitializeMutex() + { + // Mutex name should be unique for your application. + string mutexName = "Global\\MyWinUIAppMutex"; + bool createdNew; + + // Try to create the Mutex. If it fails, another instance is already running. + mutex = new Mutex(true, mutexName, out createdNew); + if (!createdNew) + { + // Another instance is already running. + Environment.Exit(0); // Exit the current application. + } + + // Clean up the Mutex when the application exits. + //Current.Exiting += (sender, args) => + //{ + // mutex.Close(); + // mutex.Dispose(); + //}; + } }