-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathApp.xaml.cs
130 lines (99 loc) · 2.76 KB
/
App.xaml.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using JetBrains.Annotations;
using Novus.Utilities;
using Novus.Win32;
#nullable disable
namespace SmartImage.UI;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// This identifier must be unique for each application.
/// </summary>
public const string SINGLE_GUID = "{910e8c27-ab31-4043-9c5d-1382707e6c93}";
public const string IPC_PIPE_NAME = "SIPC";
public const char ARGS_DELIM = '\0';
private static Mutex _singleMutex;
public NamedPipeServerStream PipeServer { get; private set; }
public Thread PipeThread { get; private set; }
private void Application_Startup(object sender, StartupEventArgs startupArgs)
{
bool multipleInstances = false, pipeServer = true;
var enumerator = startupArgs.Args.GetEnumerator();
using var unknown = enumerator as IDisposable;
while (enumerator.MoveNext()) {
var el = enumerator.Current;
var els = el?.ToString();
switch (els) {
case "-mi":
multipleInstances = true;
break;
case "-nms":
pipeServer = false;
break;
default:
break;
}
}
_singleMutex = new Mutex(true, SINGLE_GUID);
var isOnlyInstance = _singleMutex.WaitOne(TimeSpan.Zero, true);
if (multipleInstances || isOnlyInstance) {
// Show main window
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
// Release SingleInstance Mutex
_singleMutex.ReleaseMutex();
if (pipeServer) {
StartServer();
}
}
else {
// Bring the already running application into the foreground
// Native.PostMessage(0xffff, AppUtil.m_registerWindowMessage, 0, 0);
SendMessage(startupArgs);
Shutdown();
}
}
private static void SendMessage(StartupEventArgs e)
{
using var pipe = new NamedPipeClientStream(".", IPC_PIPE_NAME, PipeDirection.Out);
using var stream = new StreamWriter(pipe);
pipe.Connect();
foreach (var s in e.Args) {
stream.WriteLine(s);
}
stream.Write($"{ARGS_DELIM}{ProcessHelper.GetParent().Id}");
}
public delegate void PipeMessageCallback(string s);
public event PipeMessageCallback OnPipeMessage;
[DebuggerHidden]
private void StartServer()
{
PipeServer = new NamedPipeServerStream(IPC_PIPE_NAME, PipeDirection.In);
PipeThread = new Thread([DebuggerHidden]() =>
{
while (true) {
PipeServer.WaitForConnection();
var sr = new StreamReader(PipeServer);
while (!sr.EndOfStream) {
var v = sr.ReadLine();
OnPipeMessage?.Invoke(v);
}
PipeServer.Disconnect();
}
})
{
IsBackground = true
};
PipeThread.Start();
}
}