-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
84 lines (78 loc) · 2.71 KB
/
Program.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
using System;
using System.IO.Pipes;
using System.Threading;
using System.Windows.Forms;
using MouseNet.Logophi.Utilities;
namespace MouseNet.Logophi
{
internal static class Program
{
private static MessageReceiver _messageReceiver;
private static AppContext _app;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
//use a Mutex to see if the program is already running
//if so, send a message to the other instance to tell it
//to show the main form
//otherwise run the program
using (new Mutex(true, "LogophiMtx", out var createdNew))
if (createdNew)
Run();
else
SendMessage();
}
/// <summary>
/// Runs the main Logophi application.
/// </summary>
private static void Run()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += OnApplicationExit;
_app = new AppContext();
//create a new message receiver to listen for other instances
//of Logophi
_messageReceiver =
new MessageReceiver("LogophiMessageReceiver");
_messageReceiver.Connected += OnMessageReceiverConnected;
_messageReceiver.StartListening();
Application.Run(_app);
}
/// <summary>
/// Uses a pipe client to try to connect to a running
/// instance of Logophi, signaling that it sould display
/// its main window.
/// </summary>
private static void SendMessage()
{
using (var pipeClient =
new NamedPipeClientStream(
".",
"LogophiMessageReceiver",
PipeDirection.Out,
PipeOptions.Asynchronous))
pipeClient.Connect();
}
private static void OnApplicationExit
(object sender,
EventArgs e)
{
_messageReceiver?.Dispose();
}
private static void OnMessageReceiverConnected
(object sender,
EventArgs e)
{
//show the main window and reset the message receiver
//to continue listening for new instances of Logophi
_app.PresentMainForm();
if (_messageReceiver.IsConnected)
_messageReceiver.Disconnect();
_messageReceiver.StartListening();
}
}
}