If you do not want to allow the user to run multiple instances of your application, here‘s how to do that using the Mutex class from System.Threading :
// Setup a unique Name for your Application
static Mutex mutex = new Mutex(false, "My_Unique_App_ID");
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
// Delay 3 seconds to be sure that there is no other instance running
if (!mutex.WaitOne(TimeSpan.FromSeconds(3), false))
{
MessageBox.Show("Another instance of this application is already running");
return;
}
try
{
// There is no other instances running
// Launch the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
finally
{
mutex.ReleaseMutex();
}
}
The Mutex will be released when the application ends, but even if the application terminates without releasing the Mutex, it will be automatically released by the .net framework.