How to Prevent duplicate execute of my program ?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 2
Reputation: danielc is an unknown quantity at this point 
Solved Threads: 0
danielc danielc is offline Offline
Newbie Poster

How to Prevent duplicate execute of my program ?

 
0
  #1
Mar 28th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: How to Prevent duplicate execute of my program ?

 
0
  #2
Mar 28th, 2008
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...
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: Reinard is an unknown quantity at this point 
Solved Threads: 1
Reinard Reinard is offline Offline
Newbie Poster

Re: How to Prevent duplicate execute of my program ?

 
0
  #3
Mar 31st, 2008
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();
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: termi65 is an unknown quantity at this point 
Solved Threads: 0
termi65 termi65 is offline Offline
Newbie Poster

Re: How to Prevent duplicate execute of my program ?

 
1
  #4
Mar 31st, 2008
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: Reinard is an unknown quantity at this point 
Solved Threads: 1
Reinard Reinard is offline Offline
Newbie Poster

Re: How to Prevent duplicate execute of my program ?

 
0
  #5
Mar 31st, 2008
Originally Posted by termi65 View Post
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!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: How to Prevent duplicate execute of my program ?

 
0
  #6
Mar 31st, 2008
Advantage of that later is you cannot simply change the name of the executable and then run two instances of the application.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 75
Reputation: Sailor_Jerry is an unknown quantity at this point 
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: How to Prevent duplicate execute of my program ?

 
0
  #7
Dec 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: How to Prevent duplicate execute of my program ?

 
0
  #8
Dec 23rd, 2008
Use this sample...
  1. Process[] processes = Process.GetProcessesByName("C");
  2. foreach (Process p in processes)
  3. {
  4. MessageBox.Show("Can't duplicate run this program,it is already running in the background.", "can't run again", MessageBoxButtons.OK, MessageBoxIcon.Error);
  5. this.Close();
  6. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 58
Reputation: vckicks is an unknown quantity at this point 
Solved Threads: 9
vckicks vckicks is offline Offline
Junior Poster in Training

Re: How to Prevent duplicate execute of my program ?

 
0
  #9
Dec 25th, 2008
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
Visual C# Kicks - Free C# code resources and articles.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 323
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: How to Prevent duplicate execute of my program ?

 
0
  #10
Jan 1st, 2009
I like to use a slight variation, just modify your entry point as follows

  1. static void Main()
  2. {
  3. string proc = Process.GetCurrentProcess().ProcessName;
  4. Process[] processes = Process.GetProcessesByName(proc);
  5. if (processes.Length > 1)
  6. {
  7. MessageBox.Show("Application is already running\n\nOnly instance open at a time.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  8. }
  9. else
  10. {
  11. Application.EnableVisualStyles();
  12. Application.Run(new Form1());
  13. }
  14. }

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC