Single instance application.
We can use two ways to solve this problem.
1. Using Mutex
2. Using Process list and checking for the any other running instance
Option1
static void Main() {
bool running;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "applicationName", out running);
if(!running) {
MessageBox.Show("Another instance is already running.");
return;
}
Application.Run(new Form1());
GC.KeepAlive(mutex);
}
Option2
static void Main() {
bool flag;
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs) {
if ((p.Id != curr.Id) && (p.MainModule.FileName == curr.MainModule.FileName)) {
flag = true;
break;
}
}
if(flag) {
MessageBox.Show("Application already running");
return;
}
Application.Run(new Form1());
}
Monday, May 25, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment