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

前端 未结 30 3109
耶瑟儿~
耶瑟儿~ 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:56

    I found the simpler solution, similar to Dale Ragan's, but slightly modified. It does practically everything you need and based on the standard Microsoft WindowsFormsApplicationBase class.

    Firstly, you create SingleInstanceController class, which you can use in all other single-instance applications, which use Windows Forms:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.ApplicationServices;
    
    
    namespace SingleInstanceController_NET
    {
        public class SingleInstanceController
        : WindowsFormsApplicationBase
        {
            public delegate Form CreateMainForm();
            public delegate void StartNextInstanceDelegate(Form mainWindow);
            CreateMainForm formCreation;
            StartNextInstanceDelegate onStartNextInstance;
            public SingleInstanceController(CreateMainForm formCreation, StartNextInstanceDelegate onStartNextInstance)
            {
                // Set whether the application is single instance
                this.formCreation = formCreation;
                this.onStartNextInstance = onStartNextInstance;
                this.IsSingleInstance = true;
    
                this.StartupNextInstance += new StartupNextInstanceEventHandler(this_StartupNextInstance);                      
            }
    
            void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
            {
                if (onStartNextInstance != null)
                {
                    onStartNextInstance(this.MainForm); // This code will be executed when the user tries to start the running program again,
                                                        // for example, by clicking on the exe file.
                }                                       // This code can determine how to re-activate the existing main window of the running application.
            }
    
            protected override void OnCreateMainForm()
            {
                // Instantiate your main application form
                this.MainForm = formCreation();
            }
    
            public void Run()
            {
                string[] commandLine = new string[0];
                base.Run(commandLine);
            }
        }
    }
    

    Then you can use it in your program as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using SingleInstanceController_NET;
    
    namespace SingleInstance
    {
        static class Program
        {
            /// 
            /// The main entry point for the application.
            /// 
            static Form CreateForm()
            {
                return new Form1(); // Form1 is used for the main window.
            }
    
            static void OnStartNextInstance(Form mainWindow) // When the user tries to restart the application again,
                                                             // the main window is activated again.
            {
                mainWindow.WindowState = FormWindowState.Maximized;
            }
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            
                SingleInstanceController controller = new SingleInstanceController(CreateForm, OnStartNextInstance);
                controller.Run();         
            }
        }
    }
    

    Both the program and the SingleInstanceController_NET solution should reference Microsoft.VisualBasic . If you just want to reactivate the running application as a normal window when the user tries to restart the running program, the second parameter in the SingleInstanceController can be null. In the given example, the window is maximized.

提交回复
热议问题