|
-
September 19th, 2005, 11:28 PM
#1
Prevent an Application starting more than once
Is there a way to prevent a user from starting the same application more than once?
-
September 19th, 2005, 11:59 PM
#2
Re: Prevent an Application starting more than once
I found this code.
Code:
Process ThisProcess = Process.GetCurrentProcess();
Process[] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
if (AllProcesses.Length > 1)
{
MessageBox.Show(ThisProcess.ProcessName + " is already running",
ThisProcess.ProcessName,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
You can put this in your Main method and it should work.
Jason Isom
.NET 2.0 / VS2005 Developer
-
September 20th, 2005, 12:00 AM
#3
Re: Prevent an Application starting more than once
On startup you can use the FindWindow API call to check if any windows with same name are active.
-
September 20th, 2005, 12:24 AM
#4
Re: Prevent an Application starting more than once
Jason, Do I place this code in the Load Event of the form?
Sahir, is there some sample code for the FindWindow way?
-
September 20th, 2005, 01:09 AM
#5
Re: Prevent an Application starting more than once
put this code in the main function:
Code:
using System.Threading
using System.Runtime.InteropServices;
public class Form1 : Form
{
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "YourAppName", out createdNew);
if (! createdNew)
{
// app is already running...
MessageBox.Show("Only one instance of this application is allowed at a time.");
return;
}
Application.Run(new Form1());
// keep the mutex reference alive until the normal termination of the program
GC.KeepAlive(m);
}
}
Make sure not to forget putting your application name instead of "YourAppName" above (in the mutex decleration).
The next section will close the PREVIOUS instance of the application:
Code:
using System;
using System.ComponentModel;
using System.Diagnostics;
static void Main()
{
// get the name of the current process
Process currentProcess = Process.GetCurrentProcess();
string currProcessName = currentProcess.ProcessName;
// get all instances of the this application
Process[] prevAppInstance = Process.GetProcessesByName(currProcessName);
// kill all other instances of this application
for(int i = 0 ; i<prevAppInstance.GetLength(0) ; i++)
{
if(prevAppInstance[i].Id != currentProcess.Id)
{
prevAppInstance[i].Kill();
}
}
// run the aplication
Application.Run(new Form1());
}
Last edited by jhammer; September 20th, 2005 at 05:51 AM.
-
September 20th, 2005, 01:20 AM
#6
Re: Prevent an Application starting more than once
jhammer,
Thanks for that. Which one do you prefer to use? The first one or the second one?
-
September 20th, 2005, 01:59 AM
#7
Re: Prevent an Application starting more than once
 Originally Posted by CSD
Sahir, is there some sample code for the FindWindow way?
Yes you probably can find plenty of examples in MSDN and here on Codeguru as well, but I think Jason's suggestion is a better one. Do let us know if it works.
-
September 20th, 2005, 05:49 AM
#8
Re: Prevent an Application starting more than once
 Originally Posted by CSD
Which one do you prefer to use? The first one or the second one?
Well, that depends on your needs. If you want to run an application and than prevent running it again, then the first one will do.
If you want to kill the current instance running, and run it all over again, then go for the second one.
-
September 20th, 2005, 08:48 PM
#9
Re: Prevent an Application starting more than once
jhammer,
I have implemeted your first option and it works very well. It is exactly what I needed.
Cheers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|