Click here to Skip to main content
15,889,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I need a piece of code that checks whether one or more instance of exe is running or not. If so, it should not allow to run. If I run my application directly, it provides me appropriate message but whereas if I run my command prompt it doesn't. How to stop it if it is running in command prompt. Please help.

What I have tried:

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
                {
                   Console.WriteLine("App is already running on this computer. Only one instance of App can run at a time");
                    return;
                }
Posted
Updated 31-Jan-17 0:43am

 
Share this answer
 
Try this:
C#
Process thisProcess = Process.GetCurrentProcess();
if (Process.GetProcessesByName(thisProcess.ProcessName).Count() > 1)
    {
    Console.WriteLine("Allready running");
    }
 
Share this answer
 
This is an often asked question:

How to prevent an application to be started multiple times?

There are multiple possible answers but the best is:

Use a mutex.

Examples can be found by searching the web like
C# Developing.net - How to prevent opening multiple instance of application[^]
The Misunderstood Mutex[^]

You may also read this SO thread because it discusses also if the application should be single for all users or the active one only.
c# - Is using a Mutex to prevent multiple instances of the same program from running safe? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900