-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (104 loc) · 4.29 KB
/
Program.cs
File metadata and controls
117 lines (104 loc) · 4.29 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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DefaultBrowserWindows
{
internal static class Program
{
private const string PIPE_NAME = "DefaultBrowserWindowsPipe";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var args = Environment.GetCommandLineArgs();
// Check if another instance is already running
bool createdNew;
using (var mutex = new System.Threading.Mutex(true, "DefaultBrowserWindows", out createdNew))
{
if (!createdNew)
{
// Another instance is running, send the command line arguments to it
if (args.Length > 1)
{
SendArgsToExistingInstance(args);
}
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Run the application
using (var trayApp = new TrayApplication())
{
// Start listening for commands from other instances
StartNamedPipeServer(trayApp);
Application.Run();
}
}
}
private static void SendArgsToExistingInstance(string[] args)
{
try
{
using (var client = new NamedPipeClientStream(".", PIPE_NAME, PipeDirection.Out))
{
client.Connect(1000); // 1 second timeout
var message = string.Join("|", args.Skip(1)); // Skip the executable path
var data = Encoding.UTF8.GetBytes(message);
client.Write(data, 0, data.Length);
client.Flush();
}
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to send args to existing instance: {ex.Message}");
// If we can't communicate with the existing instance, just exit silently
}
}
private static void StartNamedPipeServer(TrayApplication trayApp)
{
Task.Run(async () =>
{
while (true)
{
try
{
using (var server = new NamedPipeServerStream(PIPE_NAME, PipeDirection.In))
{
await server.WaitForConnectionAsync();
var buffer = new byte[1024];
int bytesRead = await server.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
var args = message.Split('|');
// Handle the arguments on the UI thread
try
{
trayApp.Invoke(new Action(() => trayApp.HandleExternalCommand(args)));
}
catch (Exception invokeEx)
{
Debug.WriteLine($"Error invoking command handler: {invokeEx.Message}");
// Fallback: try to handle on current thread
trayApp.HandleExternalCommand(args);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Named pipe server error: {ex.Message}");
await Task.Delay(1000); // Wait before retrying
}
}
});
}
}
}