Fork me on GitHub

Metux 实现单例进程

WPF 单例

 

 public abstract class SingletonApplication : Application
    {
        private Mutex _singleTonApp = null;

        protected SingletonApplication(string appName)
        {
            _singleTonApp = new Mutex(true, appName, out var createdNew);
            if (createdNew)
            {
                // ReSharper disable once VirtualMemberCallInConstructor
                CreatedNewApp();
            }
            else
            {
                // ReSharper disable once VirtualMemberCallInConstructor
                UpdateApp();
            }
        }

        /// <summary>
        /// 首次运行app
        /// </summary>
        protected abstract void CreatedNewApp();
        /// <summary>
        /// 已经有app在运行
        /// </summary>
        protected abstract void UpdateApp();
    }

  

posted @ 2023-02-21 10:18  黄高林  阅读(40)  评论(0编辑  收藏  举报