Original: WPF: How to implement a single-instance application (Singleton Instance)
Well, this is the fourth article in a series of articles that I'll compare WPF with Windows Forms to discuss how to implement single instance
Let's take a look at the first of the simplest and most outrageous practices:
detects the process name and, if the name is the same, indicates that the program has started and is no longer started.
protected Override void Onstartup (StartupEventArgs e) { //Get Referenceto the current process process Thisproc = Process.getcurrentprocess (); //Check How many total processes has the same name as the current one if (Process.getprocessesbyname (Thisproc.processname). Length > 1) { //If ther is more than one, than it is already running. MessageBox.Show ("application is already running."); Application.Current.Shutdown (); return; } base. Onstartup (e); } It's simple, isn't it? But what's wrong with simplicity? It's very practical.
[note] This code is not valid if debugging in Visual Studio, because the process used by Visual Studio debugging is added with a vshost suffix.
?
The second scenario, I think it should still be possible to implement with a mutex , look at the following code
usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Windows;usingSystem.Diagnostics;usingSystem.Threading;namespacewpfapplication1{/// /// App.xaml's interactive logic /// Public Partial classapp:application {protected Override voidOnstartup (StartupEventArgs e) {BOOLCreateNew; Mutex Mutex =NewMutex (true,"MyApplication", outCREATENEW);if(CREATENEW)Base. Onstartup (e);Else{MessageBox.Show ("The program has started."); Application.Current.Shutdown (); } } }}
The result of this approach is similar to the first, or no difference.
?
Seems to solve the problem, but still not very ideal. The best case scenario is that when the user opens the second instance, it should be activated if the first instance is not active.
It is natural to think of the original windowsformsapplicationbasein the Windows Forms era, which is too simple to do.
First, add a reference to the Microsoft.VisualBasic
namespacewpfapplication1{ Public classentrypoint {[STAThread] Public Static voidMain (string[] args) {Singleinstancemanager manager =NewSingleinstancemanager (); Manager. Run (args); } }//Using VB bits to detect a single instances and process accordingly: //* Onstartup is fired when the first instance loads //* Onstartupnextinstance is fired if the application is re-run again //Note:it is redirected to the instance thanks to Issingleinstance Public classsingleinstancemanager:windowsformsapplicationbase {singleinstanceapplication app; PublicSingleinstancemanager () { This. Issingleinstance =true; }protected Override BOOLOnstartup (Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e) {// first time app is launchedApp =NewSingleinstanceapplication (); App. Run ();return false; }protected Override voidOnstartupnextinstance (Startupnextinstanceeventargs EventArgs) {//Subsequent launches Base. Onstartupnextinstance (EventArgs); App. Activate (); } } Public classsingleinstanceapplication:application {protected Override voidOnstartup (System.Windows.StartupEventArgs e) {Base. Onstartup (e);//Create and show the application ' s main window //mainwindow window = new MainWindow ();Window1 window =NewWindow1 (); Window. Show (); } Public voidActivate () {//Reactivate application ' s main window This. Mainwindow.show (); This. Mainwindow.activate (); } }}
WPF: How to implement a single-instance application (Singleton Instance)