forked from bytecode77/bytecode-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleInstance.cs
More file actions
163 lines (151 loc) · 5.44 KB
/
SingleInstance.cs
File metadata and controls
163 lines (151 loc) · 5.44 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using BytecodeApi.Extensions;
using BytecodeApi.Mathematics;
using BytecodeApi.Threading;
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Windows;
namespace BytecodeApi.UI
{
/// <summary>
/// Class for managing single instance UI applications. A second instance can detect an already running instance and notify the first instance.
/// </summary>
public class SingleInstance : IDisposable
{
private readonly Mutex Mutex;
private readonly HwndBroadcast Broadcast;
private readonly string PipeName;
private readonly NamedPipeServerStream Pipe;
private readonly Thread PipeThread;
private readonly int PipeIdentifier;
/// <summary>
/// Occurs when <see cref="SendActivationMessage" /> is called by another running instance.
/// </summary>
public event EventHandler Activated;
/// <summary>
/// Occurs when <see cref="SendMessage" /> is called by another running instance.
/// </summary>
public event EventHandler<string> MessageReceived;
/// <summary>
/// Initializes a new instance of the <see cref="SingleInstance" /> class and registers a <see cref="System.Threading.Mutex" /> and a WindowMessage using the specified identifier.
/// </summary>
/// <param name="identifier">A <see cref="string" /> representing the identifier for the <see cref="System.Threading.Mutex" /> and the WindowMessage.</param>
public SingleInstance(string identifier)
{
Check.ArgumentNull(identifier, nameof(identifier));
Check.ArgumentEx.StringNotEmpty(identifier, nameof(identifier));
Mutex = new Mutex(false, "BAPI_SINGLE_INSTANCE_" + identifier);
Broadcast = new HwndBroadcast("BAPI_SINGLE_INSTANCE_BROADCAST_" + identifier);
Broadcast.Notified += Broadcast_Notified;
PipeName = "BAPI_SINGLE_INSTANCE_PIPE_" + identifier;
Pipe = new NamedPipeServerStream(PipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances);
PipeThread = ThreadFactory.StartThread(PipeThreadFunc);
PipeIdentifier = MathEx.RandomNumberGenerator.GetInt32();
}
/// <summary>
/// Releases all resources used by the current instance of the <see cref="SingleInstance" /> class.
/// </summary>
public void Dispose()
{
Mutex.Dispose();
Broadcast.Notified -= Broadcast_Notified;
Broadcast.Dispose();
PipeThread.Abort();
Pipe.Dispose();
}
/// <summary>
/// Registers a <see cref="Window" /> object that identifies as the main application window.
/// </summary>
/// <param name="window">The <see cref="Window" /> object identifying as the main application window.</param>
public void RegisterWindow(Window window)
{
Check.ArgumentNull(window, nameof(window));
Broadcast.RegisterWindow(window);
}
/// <summary>
/// Registers a window handle (HWND) that identifies as the main application window.
/// </summary>
/// <param name="handle">A <see cref="IntPtr" /> representing window handle (HWND).</param>
public void RegisterWindow(IntPtr handle)
{
Check.ArgumentEx.Handle(handle, nameof(handle));
Broadcast.RegisterWindow(handle);
}
/// <summary>
/// Checks whether an instance is already running by querying the <see cref="System.Threading.Mutex" />.
/// </summary>
/// <returns>
/// <see langword="true" />, if an instance is already running;
/// otherwise, <see langword="false" />.
/// </returns>
public bool CheckInstanceRunning()
{
try
{
return !Mutex.WaitOne(0, false);
}
catch (AbandonedMutexException)
{
return false;
}
}
/// <summary>
/// Sends a notification to other running instances. The <see cref="Activated" /> event will be triggered in all instances, except the current.
/// </summary>
public void SendActivationMessage()
{
Broadcast.Notify();
}
/// <summary>
/// Sends a message to other running instances. The <see cref="MessageReceived" /> event will be triggered in all instances, except the current.
/// </summary>
public void SendMessage(string message)
{
Check.ArgumentNull(message, nameof(message));
using (NamedPipeClientStream client = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
{
client.Connect();
using (StreamWriter stream = new StreamWriter(client))
{
stream.WriteLine(PipeIdentifier + ":" + message);
}
}
}
private void Broadcast_Notified(object sender, EventArgs e)
{
OnActivated(EventArgs.Empty);
}
private void PipeThreadFunc()
{
while (true)
{
Pipe.WaitForConnection();
string message = new StreamReader(Pipe).ReadToEnd();
Pipe.Disconnect();
if (message.Contains(":") && message.SubstringUntil(":").ToInt32OrDefault() is int identifier)
{
message = message.SubstringFrom(":").Trim();
if (identifier == PipeIdentifier) SendMessage(message);
else OnMessageReceived(message);
}
}
}
/// <summary>
/// Raises the <see cref="Activated" /> event.
/// </summary>
/// <param name="e">The event data for the <see cref="Activated" /> event.</param>
protected virtual void OnActivated(EventArgs e)
{
Activated?.Invoke(this, e);
}
/// <summary>
/// Raises the <see cref="MessageReceived" /> event.
/// </summary>
/// <param name="message">The message for the <see cref="MessageReceived" /> event.</param>
protected virtual void OnMessageReceived(string message)
{
MessageReceived?.Invoke(this, message);
}
}
}