This tutorial will show you how to make your windows forms application only one instance at run-time in C#.NET.
To make restrict the application to just one instance, you should get all actives process, then check your application name with the active process as the following code
public bool IsSingleInstance()
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle == this.Text)
return false;
}
return true;
}
You can see your process name in task manager
Don't forget to add a namespace to System.Diagnostics
using System.Diagnostics;
Open your program class, then check your instance as shown below
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if(IsSingleInstance())
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
Each time you open the application, we will check your application is running