This is a class to make an application where only one instance of app can be run at a time in C#. The method must be called in Program.cs by replacing:
Collapse
Application.Run(new Form1());
with Collapse
Chico.SingleApp.Initialize(true, new Form1());a shorter version(where this initial code originated from) was posted by: Ron Whittle and can be found athttp://www.daniweb.com/software-development/csharp/code/352511[^]
Collapse
using System;If you like the code or plan to use the code, please vote for me and stop by http://www.daniweb.com/software-development/csharp/code/352511[^] and take a look at the original code by Ron Whittle. Thanks and hope you like the code.
using System.Threading;
using System.Windows.Forms;
namespace Chico
{
public class SingleApp
{
private static Boolean IsSingleAppMode;
private static Mutex singleApp;
private const Int32 waitTime = 3000;
public static Boolean Initialize(Boolean value, Form form)
{
try
{
if (value)
{
using (singleApp = InitializeSingleAppMode(IsSingleAppMode))
{
if (StartAsSingleAppMode(IsSingleAppMode))
{
StartApplication(form);
}
}
}
else
{
StartApplication(form);
}
}
catch { }
return value;
}
private static Boolean StartAsSingleAppMode(Boolean result)
{
return singleApp.WaitOne(waitTime, result);
}
private static void StartApplication(Form mainForm)
{
Application.Run(mainForm);
}
private static Mutex InitializeSingleAppMode(Boolean result)
{
return new Mutex(result, "anydomain.com myprogramname", out IsSingleAppMode);
}
}
}
Posted 18 Mar '11
Yet another way to this is as follows:
Collapse
using System;Take a look there[^] to read the complete article.
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace OnlyOneInstance
{
static class Program
{
[STAThread]
static void Main()
{
bool instantiated;
/* If instantiated is true, this is the first instance of the application; else, another instance is running. */
Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
if (!instantiated)
{
MessageBox.Show("Already Running");
return;
}
Application.Run(new Form1());
GC.KeepAlive(mutex);
}
}
}
True that is another way to do the same thing. The reason that I built it the way that i have is so the code will be in a class file so that it can be included in a framework
I think I prefer this technique over the original because the original will call the Form1 constructor first, while this technique will avoid that if the program is already running.
Hmm... just a quick thought: regarding Windows 7 (and perhaps vista, too, dunno), the Mutexes should be global.
Reason for my vote of 5
good-good, very good.
Maybe this is helpful, it tries to switch to the first running instance:
Collapse
namespace UltraSimple.Win
{
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[STAThread]
static void Main()
{
Process oProcess = Process.GetCurrentProcess();
Process[] aoProcesses = Process.GetProcessesByName(oProcess.ProcessName);
if (aoProcesses.Length > 1)
{
MessageBox.Show(
"The application \""
+ oProcess.ProcessName
+ "\" is already running.", "Ultrasimple");
Program.SetForegroundWindow(aoProcesses[aoProcesses[0].Id ==
oProcess.Id ? 1 : 0].MainWindowHandle);
return;
}
...
}
}
}
I actually use code like this, which allows me to detect a existing instance and to call the original instance with the command line (I expand any filenames present so that it can access the filenames even if the original path is different).
Collapse
string processName = Process.GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);
if (instances.Length > 1)
{
//Try and send a message with args[]
StringBuilder text = new StringBuilder();
foreach (string str in args)
{
if (File.Exists(str)) //If it is a file, I want full path
{
text.Append(new FileInfo(str).FullName);
}
else
{
text.Append(str);
}
text.Append(' ');
}
if (text.Length > 0)
{
text.Remove(text.Length - 1, 1);
}
Remote.Send(text.ToString());
// MessageBox.Show("This application is already running");
return;
}
Be aware that this Code won't work in Terminal Server Sessions.