判断是否为重新启动的代码

    public partial class App : Application
    {
        private static Mutex AppMutex;
        protected override void OnStartup(StartupEventArgs e)
        {
            AppMutex = new Mutex(true, "ZrClient", out var createdNew);

            if (!createdNew)
            {
                var current = Process.GetCurrentProcess();

                foreach (var process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        Win32Helper.SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
                Shutdown();
            }
        }
    }

 

posted @ 2024-10-11 15:30  星星c#  阅读(7)  评论(0编辑  收藏  举报