-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (102 loc) · 3.71 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (102 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 YJZ
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace FolderEncryptor
{
internal static class Program
{
private const string MutexName = "Local\\YJZFolderEncryptorSingleInstance";
private const string ActivateEventName = "Local\\YJZFolderEncryptorActivate";
private const int SW_RESTORE = 9;
[STAThread]
private static void Main()
{
bool createdNew;
using (Mutex mutex = new Mutex(true, MutexName, out createdNew))
{
if (!createdNew)
{
SignalExistingInstance();
FocusExistingProcessWindow();
return;
}
using (EventWaitHandle activateEvent = new EventWaitHandle(false, EventResetMode.AutoReset, ActivateEventName))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm form = new MainForm();
StartActivationThread(form, activateEvent);
Application.Run(form);
GC.KeepAlive(mutex);
}
}
}
private static void StartActivationThread(MainForm form, EventWaitHandle activateEvent)
{
Thread thread = new Thread(delegate()
{
while (!form.IsDisposed)
{
activateEvent.WaitOne();
if (form.IsDisposed)
return;
try
{
form.BeginInvoke(new MethodInvoker(form.ShowEncryptTabAndBringToFront));
}
catch (InvalidOperationException)
{
return;
}
}
});
thread.IsBackground = true;
thread.Name = "FolderEncryptorActivation";
thread.Start();
}
private static void SignalExistingInstance()
{
try
{
EventWaitHandle activateEvent = EventWaitHandle.OpenExisting(ActivateEventName);
activateEvent.Set();
activateEvent.Close();
}
catch (WaitHandleCannotBeOpenedException)
{
}
}
private static void FocusExistingProcessWindow()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
for (int attempt = 0; attempt < 20; attempt++)
{
foreach (Process process in processes)
{
if (process.Id == current.Id)
continue;
IntPtr handle = process.MainWindowHandle;
if (handle == IntPtr.Zero)
continue;
if (IsIconic(handle))
ShowWindow(handle, SW_RESTORE);
SetForegroundWindow(handle);
return;
}
Thread.Sleep(100);
processes = Process.GetProcessesByName(current.ProcessName);
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
}
}