Skip to content

Commit 8d74a1b

Browse files
committed
feat: 传递启动参数给当前运行的实例
1 parent 3ecc669 commit 8d74a1b

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

TuneLab/App.axaml.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
using TuneLab.Utils;
1818
using TuneLab.I18N;
1919
using System.Linq;
20+
using System.Diagnostics.CodeAnalysis;
21+
using System.Threading.Tasks;
22+
using System.Threading;
23+
using System.IO.Pipes;
2024

2125
namespace TuneLab;
2226

@@ -60,16 +64,46 @@ public override void OnFrameworkInitializationCompleted()
6064
Settings.AudioDevice.Modified.Subscribe(() => { AudioEngine.CurrentDevice.Value = Settings.AudioDevice; });
6165

6266
ExtensionManager.LoadExtensions();
63-
var mainWindow = new MainWindow();
64-
desktop.MainWindow = mainWindow;
67+
mMainWindow = new MainWindow();
68+
desktop.MainWindow = mMainWindow;
6569

6670
// 检测启动参数
6771
var args = Environment.GetCommandLineArgs();
68-
if (args.Length > 1)
72+
Log.Info($"Command line args:");
73+
for (int i = 1; i < args.Length; i++)
6974
{
70-
var filePath = args[1];
71-
mainWindow.Editor.OpenProjectByPath(filePath);
75+
Log.Info(args[i]);
76+
HandleArg(args[i]);
7277
}
78+
79+
// 获取主线程SynchronizationContext
80+
var context = SynchronizationContext.Current;
81+
if (context == null)
82+
{
83+
Log.Error("SynchronizationContext.Current is null");
84+
return;
85+
}
86+
87+
// 监听其他实例的启动参数
88+
Task.Run(() =>
89+
{
90+
while (true)
91+
{
92+
var pipeServer = new NamedPipeServerStream("TuneLab", PipeDirection.In);
93+
pipeServer.WaitForConnection();
94+
95+
using var reader = new StreamReader(pipeServer);
96+
while (pipeServer.IsConnected)
97+
{
98+
var arg = reader.ReadLine();
99+
if (arg == null)
100+
continue;
101+
102+
Log.Info($"Received from another instance: {arg}");
103+
context.Post(_ => HandleArg(arg), null);
104+
}
105+
}
106+
});
73107
}
74108
catch (Exception ex)
75109
{
@@ -100,4 +134,11 @@ public override void OnFrameworkInitializationCompleted()
100134

101135
base.OnFrameworkInitializationCompleted();
102136
}
137+
138+
public void HandleArg(string arg)
139+
{
140+
mMainWindow?.Editor.OpenProjectByPath(arg);
141+
}
142+
143+
MainWindow? mMainWindow = null;
103144
}

TuneLab/Program.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
5+
using System.IO.Pipes;
46
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
59
using Avalonia;
610
using Avalonia.Media;
711
using Avalonia.ReactiveUI;
@@ -28,7 +32,24 @@ public static void Main(string[] args)
2832
var lockFile = LockFile.Create(PathManager.LockFilePath);
2933
if (lockFile == null)
3034
{
31-
// TODO: 传递启动参数给当前运行的app
35+
try
36+
{
37+
using var pipeClient = new NamedPipeClientStream(".", "TuneLab", PipeDirection.Out);
38+
pipeClient.Connect(1000);
39+
40+
using var writer = new StreamWriter(pipeClient);
41+
foreach (var arg in args)
42+
{
43+
writer.WriteLine(arg);
44+
Log.Info($"Sent arguments to running instance: {arg}");
45+
}
46+
writer.Flush();
47+
}
48+
catch (Exception ex)
49+
{
50+
Log.Error($"Failed to send arguments to running instance: {ex}");
51+
}
52+
Log.Info("Another instance is running, exiting.");
3253
Process.GetCurrentProcess().Kill();
3354
Process.GetCurrentProcess().WaitForExit();
3455
return;

TuneLab/UI/MainWindow/MainWindow.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void UpdateTitle()
229229
Title = "TuneLab - " + mEditor.Document.Name + (mEditor.Document.IsSaved ? string.Empty : "*");
230230
}
231231

232-
void OnKeyDown(object sender, KeyEventArgs args)
232+
void OnKeyDown(object? sender, KeyEventArgs args)
233233
{
234234
if (args.KeyModifiers == KeyModifiers.None)
235235
{

0 commit comments

Comments
 (0)