Could someone show how it is possible to check whether another instance of the program (e.g. test.exe) is running and if so stop the application from loading if there is an existing instance of it.
-
3See http://stackoverflow.com/questions/94274/return-to-an-already-open-application-when-a-user-tries-to-open-a-new-instance and http://stackoverflow.com/questions/184084/how-to-force-c-net-app-to-run-only-one-instance-in-windows – ChrisF Jun 17 '11 at 21:27
-
1http://www.codeproject.com/KB/cs/restricting_instances.aspx – Tim Schmelter Jun 17 '11 at 21:28
-
1@Tim Schmelter Thanks, but this is for a GUI app, mine is a console app – Tom Jun 17 '11 at 21:31
5 Answers
Want some serious code? Here it is.
var exists = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1;
This works for any application (any name) and will become true
if there is another instance running of the same application.
Edit: To fix your needs you can use either of these:
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return;
from your Main method to quit the method... OR
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();
which will kill the currently loading process instantly.
You need to add a reference to System.Core.dll for the .Count()
extension method. Alternatively, you can use the .Length
property.

- 8,931
- 15
- 66
- 106
-
Returning in your second snippet just makes both applications die, so now there's 0 running instead of 2. – dsp_099 Dec 09 '14 at 16:24
-
@dsp_099 Unless they both start up at pretty much exactly the same time or someone runs this code in an infinite loop somehow, they won't. You can clearly see `.Count > 1` there. – Vercas Dec 09 '14 at 21:53
-
Yeah, didn't work well for me as the two applications are executed via msconfig / autostart, always launched double – dsp_099 Dec 14 '14 at 18:38
-
@dsp_099 In this case, mutexes are required for proper detection and handling. There is plenty of information on the subject. – Vercas Dec 16 '14 at 23:44
-
1
It's not sure what you mean with 'the program', but if you want to limit your application to one instance then you can use a Mutex to make sure that your application isn't already running.
[STAThread]
static void Main()
{
Mutex mutex = new System.Threading.Mutex(false, "MyUniqueMutexName");
try
{
if (mutex.WaitOne(0, false))
{
// Run the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
MessageBox.Show("An instance of the application is already running.");
}
}
finally
{
if (mutex != null)
{
mutex.Close();
mutex = null;
}
}
}

- 13,536
- 8
- 56
- 77
-
What happens if your program is "hard killed"? Say if the program crashed or process explorer was used to kill the app in an "unfriendly manner"? Would the only way to resolve it be resetting the pc? – TamusJRoyce Mar 10 '14 at 17:19
-
I will give this method as most likely working across multiple users. Might be a better method for things like windows services, mono on other platforms, and such. – TamusJRoyce Mar 10 '14 at 17:40
-
2@TamusJRoyce No, if you read the MSDN documentation, it clearly states that if a Mutex is abandoned, the next waiting thread will get ownership of it. – Patrik Svensson Mar 11 '14 at 09:47
-
3This is the right answer, other answers did not work for ex. if i copied exe with another name. – Mhmd Sep 16 '14 at 18:40
-
1
-
-
2"limit your application to one instance" mis-leads as the second instance is running - it just has not gone far. Still a good approach to detect if another instance is running. – chux - Reinstate Monica Nov 16 '15 at 15:58
-
Tried the RunningIntance() solution first which worked but not on a terminal server with multiple users. This solution filter by user which was perfect, thank you. – Anthony Griggs Jul 12 '19 at 14:59
-
-
If I remember correctly from 7 years ago, I meant that when Main() returns, all resources are closed, so what is the added value of try, finally, to "close" the mutex object here? – Roland Jan 02 '23 at 10:22
-
I meant that it's nice to be a good citizen and clean up after ourselves, even if the OS will do it at some point in time. – Patrik Svensson Jan 03 '23 at 11:58
-
On linux, use the Global prefix for your mutex name if you want it to be system-wide: @"Global\MyUniqueMutexName" – Olle Sjögren Aug 24 '23 at 14:17
Here are some good sample applications. Below is one possible way.
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.
Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
if (MainForm.RunningInstance() != null)
{
MessageBox.Show("Duplicate Instance");
//TODO:
//Your application logic for duplicate
//instances would go here.
}
Many other possible ways. See the examples for alternatives.
EDIT 1: Just saw your comment that you have got a console application. That is discussed in the second sample.

- 46,289
- 20
- 116
- 131
-
An upvote+ as this worked, but unfortunately in my case I needed to run it on a terminal server. Since it is not user specific it failed in that scenario as only one user on the server could run it at a time. – Anthony Griggs Jul 12 '19 at 13:16
The Process static class has a method GetProcessesByName() which you can use to search through running processes. Just search for any other process with the same executable name.

- 70,210
- 21
- 112
- 164
You can try this
Process[] processes = Process.GetProcessesByName("processname");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
// Do something with the handle...
//
}

- 1,301
- 9
- 10