How to prevent a user from launching multiple instances of an application - .Net C# Code
using System;
using System.Windows.Forms ;
using System.Collections.Generic;
namespace
{
{
/// Define the Mutex
/// </summary>
private static System.Threading. Mutex PreviousInstance;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application .EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false );
PreviousInstance = new System.Threading.Mutex(true , "NetBuddy");//Create a Mutexif (PreviousInstance.WaitOne(0, false)) //Check for Existing Mutex
{
Application.EnableVisualStyles(); //Enable XP/Vista Visual Styles
Application.Run(new frmMain()); //Run Main Form
}
else
{
MessageBox .Show("You are trying to launch multiple Instance of NetBuddy." + Environment .NewLine + "This instance will terminate now.", "NetBuddy Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);//Warn User that another instance is running
}
}
}