Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
130 lines (116 loc) · 4.17 KB
/
Copy pathProgram.cs
File metadata and controls
130 lines (116 loc) · 4.17 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
using System;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
namespace AIPaste;
static class Program
{
private const string PipeName = "AIPasteSingleInstancePipe";
private static Mutex? _mutex;
private static Thread? _pipeServerThread;
private static bool _keepRunning = true;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Pin the bundled Copilot CLI to the version shipped with the SDK by disabling
// its self-update, so the CLI/SDK protocol versions cannot drift apart. Set
// process-wide so every child copilot.exe (SDK-spawned or the login launch)
// inherits it via the environment block.
Environment.SetEnvironmentVariable("COPILOT_AUTO_UPDATE", "false");
ApplicationConfiguration.Initialize();
// Try to get a single-instance lock using a mutex
_mutex = new Mutex(true, "Global\\" + PipeName, out bool createdNew);
if (!createdNew)
{
// Another instance is already running, send a message to it
SendMessage("SHOW");
return;
}
// Start the pipe server in a separate thread
_pipeServerThread = new Thread(PipeServerThread);
_pipeServerThread.IsBackground = true;
_pipeServerThread.Start();
// Create main form
AIPaste.UI.Theme.ApplyMode(ConfigManager.GetThemeMode());
var mainForm = new MainForm(true);
try
{
// Run the application
Application.Run(mainForm);
}
finally
{
// Clean up resources
_keepRunning = false;
_mutex.ReleaseMutex();
_mutex.Dispose();
}
}
private static void PipeServerThread()
{
while (_keepRunning)
{
try
{
using (var pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In))
{
// Wait for connection from another instance
pipeServer.WaitForConnection();
// Read the message
using (var reader = new StreamReader(pipeServer))
{
string message = reader.ReadLine() ?? string.Empty;
// Handle the message
if (message == "SHOW")
{
// Use Invoke to access UI thread
var form = Application.OpenForms[0];
if (form != null)
{
form.Invoke(new Action(() => {
if (form is MainForm mainForm)
{
mainForm.OpenClipboardPopupPublic();
}
}));
}
}
}
}
}
catch (Exception)
{
// Pipe was broken or another error occurred
// Wait a bit before trying again
Thread.Sleep(100);
}
}
}
private static void SendMessage(string message)
{
try
{
using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
{
// Connect to the pipe server
pipeClient.Connect(1000); // 1 second timeout
// Send the message
using (var writer = new StreamWriter(pipeClient) { AutoFlush = true })
{
writer.WriteLine(message);
}
}
}
catch (Exception)
{
// Failed to communicate with the existing instance
// The existing instance might have closed or is not responding
}
}
}