|
17 | 17 | using TuneLab.Utils; |
18 | 18 | using TuneLab.I18N; |
19 | 19 | using System.Linq; |
| 20 | +using System.Diagnostics.CodeAnalysis; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using System.Threading; |
| 23 | +using System.IO.Pipes; |
20 | 24 |
|
21 | 25 | namespace TuneLab; |
22 | 26 |
|
@@ -60,16 +64,46 @@ public override void OnFrameworkInitializationCompleted() |
60 | 64 | Settings.AudioDevice.Modified.Subscribe(() => { AudioEngine.CurrentDevice.Value = Settings.AudioDevice; }); |
61 | 65 |
|
62 | 66 | ExtensionManager.LoadExtensions(); |
63 | | - var mainWindow = new MainWindow(); |
64 | | - desktop.MainWindow = mainWindow; |
| 67 | + mMainWindow = new MainWindow(); |
| 68 | + desktop.MainWindow = mMainWindow; |
65 | 69 |
|
66 | 70 | // 检测启动参数 |
67 | 71 | var args = Environment.GetCommandLineArgs(); |
68 | | - if (args.Length > 1) |
| 72 | + Log.Info($"Command line args:"); |
| 73 | + for (int i = 1; i < args.Length; i++) |
69 | 74 | { |
70 | | - var filePath = args[1]; |
71 | | - mainWindow.Editor.OpenProjectByPath(filePath); |
| 75 | + Log.Info(args[i]); |
| 76 | + HandleArg(args[i]); |
72 | 77 | } |
| 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 | + }); |
73 | 107 | } |
74 | 108 | catch (Exception ex) |
75 | 109 | { |
@@ -100,4 +134,11 @@ public override void OnFrameworkInitializationCompleted() |
100 | 134 |
|
101 | 135 | base.OnFrameworkInitializationCompleted(); |
102 | 136 | } |
| 137 | + |
| 138 | + public void HandleArg(string arg) |
| 139 | + { |
| 140 | + mMainWindow?.Editor.OpenProjectByPath(arg); |
| 141 | + } |
| 142 | + |
| 143 | + MainWindow? mMainWindow = null; |
103 | 144 | } |
0 commit comments