943,871 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 10792
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 28th, 2008
0

How to Prevent duplicate execute of my program ?

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danielc is offline Offline
2 posts
since Mar 2008
Mar 28th, 2008
0

Re: How to Prevent duplicate execute of my program ?

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...
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Mar 31st, 2008
0

Re: How to Prevent duplicate execute of my program ?

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();
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Reinard is offline Offline
7 posts
since Mar 2008
Mar 31st, 2008
1

Re: How to Prevent duplicate execute of my program ?

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);
}
}
Reputation Points: 12
Solved Threads: 0
Newbie Poster
termi65 is offline Offline
1 posts
since Mar 2008
Mar 31st, 2008
0

Re: How to Prevent duplicate execute of my program ?

Click to Expand / Collapse  Quote originally posted by termi65 ...
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!!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Reinard is offline Offline
7 posts
since Mar 2008
Mar 31st, 2008
0

Re: How to Prevent duplicate execute of my program ?

Advantage of that later is you cannot simply change the name of the executable and then run two instances of the application.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Dec 23rd, 2008
0

Re: How to Prevent duplicate execute of my program ?

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.
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
Sailor_Jerry is offline Offline
88 posts
since Aug 2005
Dec 23rd, 2008
0

Re: How to Prevent duplicate execute of my program ?

Use this sample...
c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Dec 25th, 2008
0

Re: How to Prevent duplicate execute of my program ?

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
Reputation Points: 11
Solved Threads: 9
Junior Poster in Training
vckicks is offline Offline
58 posts
since Jun 2008
Jan 1st, 2009
0

Re: How to Prevent duplicate execute of my program ?

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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: spss to c#
Next Thread in C# Forum Timeline: Typecasting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC