When I call the Application.Restart() method, the error comes up that detects whether the application is currently running. Is there anyway around this?
static void Main(string[] args)
{
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length > 1)
{
MessageBox.Show("Program is already running.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
The most effective way to do this is to inherit from VB.Net’s
WindowsFormsApplicationBaseclass, which can also be used in C#, and set theIsSingleInstanceproperty totrue. This uses a mutex and will continue working if the EXE file is renamed.