WPF Single Instance Best Practices

前端 未结 11 1538
名媛妹妹
名媛妹妹 2020-12-07 08:19

This is the code I implemented so far to create a single instance WPF application:

#region Using Directives
using System;
using System.Globalization;
using S         


        
11条回答
  •  情歌与酒
    2020-12-07 09:20

    For WPF just use:

    public partial class App : Application
    {
        private static Mutex _mutex = null;
    
        protected override void OnStartup(StartupEventArgs e)
        {
            const string appName = "MyAppName";
            bool createdNew;
    
            _mutex = new Mutex(true, appName, out createdNew);
    
            if (!createdNew)
            {
                //app is already running! Exiting the application  
                Application.Current.Shutdown();
            }
    
            base.OnStartup(e);
        }          
    }
    

提交回复
热议问题