This code is used to prevent Multiple instances of same application.I googled a day for this.
//Add these References
using System.Threading;
using
System.Reflection;
using
System.Runtime.InteropServices;
Simple way to Prevent Multiple Instance.Write this code in Program.cs,
[STAThread]
static
void Main()
{
bool NewWindow;
Mutex Mut = new Mutex(true, "YourApplicationName", out
NewWindow);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if
(!NewWindow)
return;
Application.Run(new Form1());
GC.KeepAlive(Mut);
}
This is the another way,
[STAThread]
static
void Main()
{
bool NewWindow;
Mutex Mut = new Mutex(true, "YourApplicationName", out
NewWindow);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if
(!NewWindow)
{
IntPtr hWnd =
FindWindow("WindowsForms10.Window.8.app3", "YourApplicationName");
if (hWnd != IntPtr.Zero)
{
WINDOWPLACEMENT placement = new
WINDOWPLACEMENT();
placement.length =
Marshal.SizeOf(placement);
GetWindowPlacement(hWnd, ref
placement);
if (placement.showCmd !=
SW_NORMAL)
{
placement.showCmd = SW_RESTORE;
SetWindowPlacement(hWnd, ref
placement);
SetForegroundWindow(hWnd);
}
}
return;
}
Application.Run(new Form1());
GC.KeepAlive(Mut);
}

Join the conversation! Your thoughts help the community grow.