Skip to content

Commit 29498b0

Browse files
committed
Wait for the port to be read
1 parent 49a24d3 commit 29498b0

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/ElectronNET.API/Runtime/Services/ElectronProcess/ElectronProcessActive.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,31 @@ protected override Task StopCore()
155155
return Task.CompletedTask;
156156
}
157157

158-
private void Read_SocketIO_Port(object sender, string line)
158+
private async Task StartInternal(string startCmd, string args, string directoriy)
159159
{
160-
// Look for "Electron Socket: listening on port %s at"
161-
var prefix = "Electron Socket: listening on port ";
160+
var tcs = new TaskCompletionSource();
162161

163-
if (line.StartsWith(prefix))
164-
{
165-
var start = prefix.Length;
166-
var end = line.IndexOf(' ', start + 1);
167-
var port = line[start..end];
162+
void Read_SocketIO_Port(object sender, string line)
163+
{
164+
// Look for "Electron Socket: listening on port %s at"
165+
var prefix = "Electron Socket: listening on port ";
168166

169-
if (int.TryParse(port, out var p))
167+
if (line.StartsWith(prefix))
170168
{
171-
// We got the port, so no more need for reading this
172-
this.process.LineReceived -= this.Read_SocketIO_Port;
173-
ElectronNetRuntime.ElectronSocketPort = p;
169+
var start = prefix.Length;
170+
var end = line.IndexOf(' ', start + 1);
171+
var port = line[start..end];
172+
173+
if (int.TryParse(port, out var p))
174+
{
175+
// We got the port, so no more need for reading this
176+
this.process.LineReceived -= Read_SocketIO_Port;
177+
ElectronNetRuntime.ElectronSocketPort = p;
178+
tcs.SetResult();
179+
}
174180
}
175181
}
176-
}
177182

178-
private async Task StartInternal(string startCmd, string args, string directoriy)
179-
{
180183
try
181184
{
182185
await Task.Delay(10.ms()).ConfigureAwait(false);
@@ -186,10 +189,10 @@ private async Task StartInternal(string startCmd, string args, string directoriy
186189

187190
this.process = new ProcessRunner("ElectronRunner");
188191
this.process.ProcessExited += this.Process_Exited;
189-
this.process.LineReceived += this.Read_SocketIO_Port;
192+
this.process.LineReceived += Read_SocketIO_Port;
190193
this.process.Run(startCmd, args, directoriy);
191194

192-
await Task.Delay(500.ms()).ConfigureAwait(false);
195+
await tcs.Task.ConfigureAwait(false);
193196

194197
Console.Error.WriteLine("[StartInternal]: after run:");
195198

0 commit comments

Comments
 (0)