|
-
June 20th, 2005, 02:23 PM
#1
Prevent runing same executable
Hi
I created a C# sharp application and it has no threads. I want prevent someone executing the same application if it is currently executing.
Thanks
Reginold
-
June 20th, 2005, 03:04 PM
#2
Re: Prevent runing same executable
Use this code in your Main() function:
static void Main()
{
string procName = Application.ProductName;
System.Diagnostics.Process[] myProcesses
= System.Diagnostics.Process.GetProcessesByName(procName);
if (myProcesses.Length > 1)
{
MessageBox.Show("Application Already Open");
return;
}
// Now start the application
Application.Run(new Form1());
}
-
June 20th, 2005, 04:05 PM
#3
Re: Prevent runing same executable
 Originally Posted by rchrishantha
Hi
I created a C# sharp application and it has no threads. I want prevent someone executing the same application if it is currently executing.
Thanks
Reginold
if your app has no threads, thats pretty much going to guarantee that no one will be able to use it, because any running app is going to be on *some* thread.
Last edited by MadHatter; June 20th, 2005 at 04:07 PM.
-
June 21st, 2005, 01:27 AM
#4
Re: Prevent runing same executable
Here is another idea:
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);
}
}
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());
}
-
June 21st, 2005, 02:41 AM
#5
Re: Prevent runing same executable
or this one,
Code:
static void Main()
Int32 isProcessRunning;
isProcessRunning = System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
if(isProcessRunning != 1)
{
MessageBox.Show("Already an Instance running");
}
else
{
Application.Run(new Form1());
}
-
June 22nd, 2005, 03:30 AM
#6
Re: Prevent runing same executable
 Originally Posted by MadHatter
if your app has no threads, thats pretty much going to guarantee that no one will be able to use it, because any running app is going to be on *some* thread.
rofl..
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
|