Esempio n. 1
0
        public static ApplicationMutex ApplicationMutex(
            this Assembly assembly, Action shutdown = null, Process mainProcess = null)
        {
            var applicationInstanceId = assembly.GetApplicationInstanceId();

            var mutex = ApplicationMutexes.CreateOrExisting(applicationInstanceId);

            var applicationMutex = new ApplicationMutex(applicationInstanceId, mutex);

            if (!applicationMutex.IsAlreadyRunning)
            {
                return(applicationMutex);
            }

            if (mainProcess == null)
            {
                mainProcess = Process.GetCurrentProcess();
            }

            var otherApplicationInstance = FindSingleInstanceProcess(mainProcess);

            if (otherApplicationInstance == null)
            {
                return(applicationMutex.AsSingle());
            }

            var handle = otherApplicationInstance.MainWindowHandle.ToInt32();

            WindowsHelper.ForceForegroundWindow(handle);

            if (shutdown != null)
            {
                shutdown();
            }

            return(applicationMutex);
        }
Esempio n. 2
0
        public void Dispose()
        {
            ApplicationMutexes.Remove(this);

            mutex.Dispose();
        }