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);
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 ).
source share