954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Prevent duplicate execute of my program ?

Hi,

I tried to use the following code to prevent duplicate execute of my program, i pub this in the form_load event procedure:


Process[] processes = Process.GetProcessesByName("C");

foreach (Process p in processes)
{

MessageBox.Show("Can't duplicate run this program,it is already running in the background.", "can't run again", MessageBoxButtons.OK, MessageBoxIcon.Error);

this.Close();

}


but then now it kill itself; also i foudn that this relies on checking the filename that it runs at: I.E if it is C.exe, i need to put Process[] processes = Process.GetProcessesByName("C") to check is it runing, but if the user changes the filename it doesn't work then , how can i fix this or is there a better way to check duplicate execute ? many thanks for your help!

danielc
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

You can create file at specific location in program load if not exists if exists it means that the application running show to the user the messagebox "can't dupl....." in form closing remove the file...

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Hi i have used this method to stop users from opening one of my applications multiple times.

string RunningProcess = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(RunningProcess);

if (processes.Length > 1)
{
MessageBox.Show("Application is already running", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
Application.ExitThread();
}

Reinard
Newbie Poster
7 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

I use the following code in the program.cs - file in C# 2.0:
static void Main()
{
// Optional
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool createdNew;

// To prevent the program to be started twice
///Create new mutex
System.Threading.Mutex appMutex = new System.Threading.Mutex(true, Application.ProductName, out createdNew);
///if creation of mutex is successful
if (createdNew)
{

Application.Run(new frmMain(false));
appMutex.ReleaseMutex();
}
else
{
/// if the app's already running
string msg = String.Format("The Program \"{0}\" is already running", Application.ProductName);
MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

termi65
Newbie Poster
1 post since Mar 2008
Reputation Points: 12
Solved Threads: 0
 

I use the following code in the program.cs - file in C# 2.0: static void Main() { // Optional Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool createdNew; // To prevent the program to be started twice ///Create new mutex System.Threading.Mutex appMutex = new System.Threading.Mutex(true, Application.ProductName, out createdNew); ///if creation of mutex is successful if (createdNew) {

Application.Run(new frmMain(false)); appMutex.ReleaseMutex(); } else { /// if the app's already running string msg = String.Format("The Program \"{0}\" is already running", Application.ProductName); MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } }


Does this work easier for you? but as they say as long as it works then its all goooood!!!

Reinard
Newbie Poster
7 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

Advantage of that later is you cannot simply change the name of the executable and then run two instances of the application.

Paul.Esson
Junior Poster
181 posts since Feb 2005
Reputation Points: 21
Solved Threads: 10
 

I have a similar issue, but my users are not actually starting the program. There is a shortcut to the program in the windows startup folder. After a few days of running the program will have multiple processes. What could be causing this if the users are not actually clicking on the .exe?
This is my first C# program, and all it does is monitor two other programs to make sure they are on at all times.

Sailor_Jerry
Junior Poster in Training
88 posts since Aug 2005
Reputation Points: 13
Solved Threads: 2
 

Use this sample...

Process[] processes = Process.GetProcessesByName("C");
foreach (Process p in processes)
{
MessageBox.Show("Can't duplicate run this program,it is already running in the background.", "can't run again", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Some interesting answers. I've always used this method:

Single-Instance Application

It's obviously not fool-proof because changing the title of a running application is not that hard.

Something else you can do is if you can get the location of the running process you are comparing, create a new FileInfo class (I think) and compare the internal name of the file. Internal names are not as easy to change.

It's late at night and I didn't double check so forgive me if the above info is a little off

vckicks
Junior Poster in Training
58 posts since Jun 2008
Reputation Points: 11
Solved Threads: 9
 

I like to use a slight variation, just modify your entry point as follows

static void Main() 
		{
            string proc = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(proc);
            if (processes.Length > 1)
            {
                MessageBox.Show("Application is already running\n\nOnly instance open at a time.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Application.EnableVisualStyles();
                Application.Run(new Form1());
            }
		}


users can get around it, but its very handy if you just want to keep accidental multiple instances from occurring.

some of those others are just as well.
Cheers.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

Termi65 uses the method Id choose, although Id fix the name in the exe, rather than use the app name, just in case someone bothers to change it.

A bit to read on it http://www.ai.uga.edu/mc/SingleInstance.html

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

hi every body
i have used this method for this program.


string RunningProcess = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(RunningProcess);
if (processes.Length > 1)
{
MessageBox.Show("Application is already running", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
Application.ExitThread();
}
else
{
//MY PROGRAM NAME IS UPDATE
if (RunningProcess == "update")
Application.Run(new Form1());
else
{
MessageBox.Show("You Dont have permission to Raneme the program","warning!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
Application.ExitThread();
}
}

hassan-golab
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You