Single instance of application forms window and how to get a link to it?

I have a Windows Forms application that allows you to run only one instance at a time. I implemented Singleton using Mutex. The application should be launched from the command line (with or without parameters). The application starts and the script terminates. The user cannot take any action.

Thus, the purpose of the application is a simple “indicator” application that simply displays some visual and graphical information for the end user. The end user can do nothing about it, just take a look. This application is for Windows forms, because the visual and graphic appearance is relatively easy to use (you can get it on top, borderless, etc.).

Simply put: How can I exit the currently running application when someone tries to start the same application with the exit command-line option?

bool quit = (args.Length > 0 && args[0] == "quit") ? true : false; using (Mutex mutex = new Mutex(false, sExeName)) { if (!mutex.WaitOne(0, true)) { if (quit) { // This is the tricky part? // How can I get reference to "previous" launced // Windows Forms application and call it Exit() method. } } else { if (!quit) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } 
+4
source share
4 answers

The .NET framework offers a very good general solution for this. Check the bottom of this article for an MSDN article . Use the StartupNextInstanceHandler () event handler to pass arbitrary commands to an executable instance, such as "quit".

+6
source

Doesn't that make things too complicated? Instead of closing the existing instance and starting a new one, can't you just reactivate the existing instance? In any case, the code below should give you some ideas on how to do this ...?

 Process thisProcess = Process.GetCurrentProcess(); Process[] allProcesses = Process.GetProcessesByName(thisProcess.ProcessName); Process otherProcess = null; foreach (Process p in allProcesses ) { if ((p.Id != thisProcess.Id) && (p.MainModule.FileName == thisProcess.MainModule.FileName)) { otherProcess = p; break; } } if (otherProcess != null) { //note IntPtr expected by API calls. IntPtr hWnd = otherProcess.MainWindowHandle; //restore if minimized ShowWindow(hWnd ,1); //bring to the front SetForegroundWindow (hWnd); } else { //run your app here } 

There is one more question about it here.

+5
source

This is a somewhat quick and dirty solution that you probably want to clarify:

 [STAThread] static void Main() { var me = Process.GetCurrentProcess(); var otherMe = Process.GetProcessesByName(me.ProcessName).Where(p => p.Id != me.Id).FirstOrDefault(); if (otherMe != null) { otherMe.Kill(); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } 

If the application instance is already running, this process is killed; otherwise, the application starts normally.

+1
source

All Articles