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()); } } }
Clack source share