The VB.Net team has already implemented the solution. You will need to depend on Microsoft.VisualBasic.dll, but if that does not bother you, then this is a good IMHO solution. See End of Next Article: Single-Copy Applications
Here are the relevant parts of the article:
1) Add a link to Microsoft.VisualBasic.dll 2) Add the following class to your project.
public class SingleInstanceApplication : WindowsFormsApplicationBase { private SingleInstanceApplication() { base.IsSingleInstance = true; } public static void Run(Form f, StartupNextInstanceEventHandler startupHandler) { SingleInstanceApplication app = new SingleInstanceApplication(); app.MainForm = f; app.StartupNextInstance += startupHandler; app.Run(Environment.GetCommandLineArgs()); } }
Open Program.cs and add the following statement:
using Microsoft.VisualBasic.ApplicationServices;
Change the class as follows:
static class Program {
source share