Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2017-01-10 21:47:52 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2017-01-10 21:47:52 +0300
commit2d2e48e07b58fe166cca31cd17cd116b826fbb41 (patch)
treeb8961fb94e387479981c80d5bf84d7c34d0fdabe /Program.cs
parenta840b0fcf413b039ce48c1f1e7f676d27f302c9d (diff)
Now you can't run two copies of app accidentally
Diffstat (limited to 'Program.cs')
-rw-r--r--Program.cs32
1 files changed, 29 insertions, 3 deletions
diff --git a/Program.cs b/Program.cs
index bda5dee7..d25745a6 100644
--- a/Program.cs
+++ b/Program.cs
@@ -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;
+ }
+ }
+ }
+ }
}
}
}