Single Instance App.cs
/*
 Santomieri Systems
 How to create an application that only alows one copy to be running at a time (single instance application) using C# (CSharp).	
 Date: 2007-10-06
*/
	static class Program
	{
		/// 
		/// The main entry point for the application.
		/// 
		[STAThread]
		static void Main(string[] args)
		{
			bool bNew = false;
			string mutexName = "Local\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
			System.Threading.Mutex mut = null;
			try
			{
				// Try and create a new named mutex
				mut = new System.Threading.Mutex(true, mutexName, out bNew);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			if (!bNew)
			{
				// Application is already running, so close.
				return;
			}

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			Application.Run(New Form1());

			mut.Close(); // Close the Mutex after the program terminates
		}
	}