diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2017-01-10 21:47:52 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2017-01-10 21:47:52 +0300 |
commit | 2d2e48e07b58fe166cca31cd17cd116b826fbb41 (patch) | |
tree | b8961fb94e387479981c80d5bf84d7c34d0fdabe /Program.cs | |
parent | a840b0fcf413b039ce48c1f1e7f676d27f302c9d (diff) |
Now you can't run two copies of app accidentally
Diffstat (limited to 'Program.cs')
-rw-r--r-- | Program.cs | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -1,21 +1,47 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using System.Runtime.InteropServices; +using System.Threading; using System.Windows.Forms; namespace com.clusterrr.hakchi_gui { static class Program { + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool SetForegroundWindow(IntPtr hWnd); + /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + bool createdNew = true; + using (Mutex mutex = new Mutex(true, "hakchi2", out createdNew)) + { + if (createdNew) + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + else + { + Process current = Process.GetCurrentProcess(); + foreach (Process process in Process.GetProcessesByName(current.ProcessName)) + { + if (process.Id != current.Id) + { + SetForegroundWindow(process.MainWindowHandle); + break; + } + } + } + } } } } |