What is the correct way to create a single-instance WPF application?

前端 未结 30 3100
耶瑟儿~
耶瑟儿~ 2020-11-21 05:14

Using C# and WPF under .NET (rather than Windows Forms or console), what is the correct way to create an application that can only be run as a single instance?

I kno

30条回答
  •  情歌与酒
    2020-11-21 05:48

    [I have provided sample code for console and wpf applications below.]

    You only have to check the value of the createdNew variable (example below!), after you create the named Mutex instance.

    The boolean createdNew will return false:

    if the Mutex instance named "YourApplicationNameHere" was already created on the system somewhere

    The boolean createdNew will return true:

    if this is the first Mutex named "YourApplicationNameHere" on the system.


    Console application - Example:

    static Mutex m = null;
    
    static void Main(string[] args)
    {
        const string mutexName = "YourApplicationNameHere";
        bool createdNew = false;
    
        try
        {
            // Initializes a new instance of the Mutex class with a Boolean value that indicates 
            // whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, 
            // and a Boolean value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.
    
            using (m = new Mutex(true, mutexName, out createdNew))
            {
                if (!createdNew)
                {
                    Console.WriteLine("instance is alreday running... shutting down !!!");
                    Console.Read();
                    return; // Exit the application
                }
    
                // Run your windows forms app here
                Console.WriteLine("Single instance app is running!");
                Console.ReadLine();
            }
    
    
        }
        catch (Exception ex)
        {
    
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
    

    WPF-Example:

    public partial class App : Application
    {
    static Mutex m = null;
    
    protected override void OnStartup(StartupEventArgs e)
    {
    
        const string mutexName = "YourApplicationNameHere";
        bool createdNew = false;
    
        try
        {
            // Initializes a new instance of the Mutex class with a Boolean value that indicates 
            // whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, 
            // and a Boolean value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.
    
            m = new Mutex(true, mutexName, out createdNew);
    
            if (!createdNew)
            {
                Current.Shutdown(); // Exit the application
            }
    
        }
        catch (Exception)
        {
            throw;
        }
    
        base.OnStartup(e);
    }
    
    
    protected override void OnExit(ExitEventArgs e)
    {
        if (m != null)
        {
            m.Dispose();
        }
        base.OnExit(e);
    }
    }
    

提交回复
热议问题