using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
namespace Mk0.Tools.SingleInstance
{
///
/// Summary description for SingleApp.
///
public class SingleApplication
{
public SingleApplication()
{
}
///
/// Imports
///
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);
///
/// GetCurrentInstanceWindowHandle
///
///
private static IntPtr GetCurrentInstanceWindowHandle()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach (Process _process in processes)
{
if (_process.Id != process.Id &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
}
return hWnd;
}
///
/// SwitchToCurrentInstance
///
private static void SwitchToCurrentInstance()
{
IntPtr hWnd = GetCurrentInstanceWindowHandle();
if (hWnd != IntPtr.Zero)
{
if (IsIconic(hWnd) != 0)
{
ShowWindow(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);
}
}
///
/// Execute a form base application if another instance already running on
/// the system activate previous one
///
/// main form
/// your app can set if single instance (true) or multi instance (false)
/// true if no previous instance is running
public static bool Run(Form frmMain, bool appSetting=true)
{
if (IsAlreadyRunning() && appSetting)
{
SwitchToCurrentInstance();
return false;
}
Application.Run(frmMain);
return true;
}
///
/// for console base application
///
///
public static bool Run()
{
if (IsAlreadyRunning())
{
return false;
}
return true;
}
///
/// check if given exe alread running or not
///
/// returns true if already running
private static bool IsAlreadyRunning()
{
string strLoc = Assembly.GetExecutingAssembly().Location;
FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
mutex = new Mutex(true, "Global\\" + sExeName, out bool bCreatedNew);
if (bCreatedNew)
{
mutex.ReleaseMutex();
}
return !bCreatedNew;
}
static Mutex mutex;
const int SW_RESTORE = 9;
}
}