WPF mutex for single app instance not working

前端 未结 7 1645
梦谈多话
梦谈多话 2020-12-30 01:24

I\'m trying to use the mutex method for only allowing one instance of my app to run. That is - I only want a max of one instance for all users on a machine. I\'ve read throu

相关标签:
7条回答
  • 2020-12-30 02:10

    I did this from this link just add the given class and a single line in you App.Xaml.cs http://wpfsingleinstance.codeplex.com/

    public partial class App : Application    
    {
      protected override void OnStartup(StartupEventArgs e) 
      {
            WpfSingleInstance.Make(); //added this is the function of that class 
            base.OnStartup(e);
      }
    }
    
    0 讨论(0)
  • 2020-12-30 02:11

    As extension sample:

    public static class Extension
    {
        private static Mutex mutex;
    
        public static bool IsOneTimeLaunch(this Application application, string uniqueName = null)
        {
            var applicationName = Path.GetFileName(Assembly.GetEntryAssembly().GetName().Name);
            uniqueName = uniqueName ?? string.Format("{0}_{1}_{2}",
                Environment.MachineName,
                Environment.UserName,
                applicationName);
    
            application.Exit += (sender, e) => mutex.Dispose();
            bool isOneTimeLaunch;
            mutex = new Mutex(true, uniqueName, out isOneTimeLaunch);
            return isOneTimeLaunch;
        }
    }
    

    App Class:

        protected override void OnStartup(StartupEventArgs e)
        {
            if (this.IsOneTimeLaunch())
            {
                base.OnStartup(e);
            }
            else
            {
                this.Shutdown();
            }
        }
    
    0 讨论(0)
  • 2020-12-30 02:18

    Here is my new code which has the answer provided by @Willem van Rumpt (and @OJ)...

    public partial class App : Application
    {
        private Mutex _instanceMutex = null;
    
        protected override void OnStartup(StartupEventArgs e)
        {
            // check that there is only one instance of the control panel running...
            bool createdNew;
            _instanceMutex = new Mutex(true, @"Global\ControlPanel", out createdNew);
            if (!createdNew)
            {
                _instanceMutex = null;
                Application.Current.Shutdown();
                return;
            }
    
            base.OnStartup(e);
        }
    
        protected override void OnExit(ExitEventArgs e)
        {          
            if(_instanceMutex != null)
                _instanceMutex.ReleaseMutex();
            base.OnExit(e);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 02:20

    You're also disposing the mutex in the same method, so the mutex only lives for the duration of the method. Store the mutex in a static field, and keep it alive for the duration of your application.

    0 讨论(0)
  • 2020-12-30 02:21

    I can suggest a much cleaner approach that also introduce the useful concept of overriding the Main method in WPF application. Also, if using your solution you take a look at the task manager, you will notice that the new instance actually reach the execution state (you can see a new process created in the list of task) and then suddenly close. The approach proposed in the post that follows will avoid this drawback too. http://blog.clauskonrad.net/2011/04/wpf-how-to-make-your-application-single.html

    0 讨论(0)
  • 2020-12-30 02:22

    You're destroying the Mutex immediately after you've created it and tested it. You need to keep the Mutex reference alive for lifetime of your application.

    Make the Mutex a member/field of your Application class. Release the mutex when your application shuts down.

    0 讨论(0)
提交回复
热议问题