How to run a single instance of a C # WinForm application?

I have a C # application that displays the login form at startup and displays the main form after user authentication. I used Mutex to limit that only one instance of my application is running. And that works just fine for the login form. After displaying the main form, this does not limit the ability to re-open the login form. I was looking for a solution with which the login screen could not be displayed after the main form is already open.

Here is my Program.cs

[STAThread] static void Main() { bool mutexCreated=true; using (Mutex mutex = new Mutex(true, "eCS", out mutexCreated)) { if (mutexCreated) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login()); } else { Process current = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(current.ProcessName)) { if (process.Id != current.Id) { XtraMessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information); SetForegroundWindow(process.MainWindowHandle); break; } } } } } 
+4
source share
4 answers

I made small changes:

 namespace CSMutex { static class Program { [STAThread] static void Main() { bool mutexCreated=true; using(Mutex mutex = new Mutex(true, "eCS", out mutexCreated)) { if (mutexCreated) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Login loging = new Login(); Application.Run(loging); Application.Run(new Main() { UserName = loging.UserName }); } else { Process current = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(current.ProcessName)) { if (process.Id != current.Id) { MessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information); //SetForegroundWindow(process.MainWindowHandle); break; } } } } } } } 

This works as expected, i.e. even when the Login form closes (and the main form of the application starts), it prevents the user from starting the application again. I decided not to create Main from Login (this I consider how your application works), and instead I pass the Main parameter. I also made a slight change to Login , so it has a UserName propertyt (the same as Main ).

+7
source

If you're fine with a link to Microsoft.VisualBasic, you can use its SingleInstance processing.

  [STAThread] static void Main(string[] args) { using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, "MyApp.SingleInstance.Mutex", out createdNew)) { MainForm = new MainDlg(); SingleInstanceApplication.Run(MainForm, StartupNextInstanceEventHandler); } } public static void StartupNextInstanceEventHandler(object sender, StartupNextInstanceEventArgs e) { MainForm.Activate(); } public class SingleInstanceApplication : WindowsFormsApplicationBase { private SingleInstanceApplication() { base.IsSingleInstance = true; } public static void Run(Form f, StartupNextInstanceEventHandler startupHandler) { SingleInstanceApplication app = new SingleInstanceApplication(); app.MainForm = f; app.StartupNextInstance += startupHandler; app.Run(Environment.GetCommandLineArgs()); } } 
0
source

If you want to limit only one instance to Form , you can do something like this:

 public static class LoginForm { private static Form _loginForm = new Form(); public static bool ShowLoginForm(){ if(_loginForm.Visible) return false; _loginForm.Show(); return true; } } 

So, if more than one client calls this method, which is only a possible method for displaying the login form, if it is already visible, it will not be executed.

-1
source
 private bool IsSingleInstance() { string szCurrentProcessName = this.ProductName; Process[] processlist = Process.GetProcesses(); foreach(Process theprocess in processlist) { string szProcessName = theprocess.MainModule.ModuleName.ToString(); if (szProcessName.Contains(szCurrentProcessName)) return false; } return true; } 
-2
source

Source: https://habr.com/ru/post/1433233/


All Articles