Skip to content

Commit 2d0e6f5

Browse files
arthurkehrwaldfreezy
authored andcommitted
Fix timeout by loss of focus issue
1 parent 653d0c1 commit 2d0e6f5

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

Runtime/MpfWrangler.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,14 @@ private async Task WaitUntilMpfReady(GrpcChannel channel, CancellationToken ct)
555555
switch (_startupBehavior)
556556
{
557557
case MpfStartupBehavior.PingUntilReady:
558-
await PingUntilResponseOrTimeout(channel, ct);
558+
Logger.Info("Attempting to connect to MPF...");
559+
var startTime = DateTime.Now;
560+
var pingResponse = await PingUntilResponseOrTimeout(channel, ct);
561+
var timeToConnect = (DateTime.Now - startTime).TotalSeconds;
562+
Logger.Info(
563+
$"Successfully connected to MPF in {timeToConnect:F2} seconds. "
564+
+ $"MPF version: {pingResponse.MpfVersion}"
565+
);
559566
break;
560567
case MpfStartupBehavior.DelayConnection:
561568
await Task.Delay(TimeSpan.FromSeconds(_connectDelay));
@@ -576,8 +583,6 @@ private async Task<PingResponse> PingUntilResponseOrTimeout(
576583
CancellationToken ct
577584
)
578585
{
579-
Logger.Info("Attempting to connect to MPF...");
580-
var startTime = DateTime.Now;
581586
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
582587
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(_connectTimeout), cts.Token);
583588
var pingTask = PingUntilResponse(channel, cts.Token);
@@ -591,11 +596,6 @@ CancellationToken ct
591596
await timeoutTask;
592597
}
593598
catch (OperationCanceledException) { }
594-
var timeToConnect = (DateTime.Now - startTime).TotalSeconds;
595-
Logger.Info(
596-
$"Successfully connected to MPF in {timeToConnect:F2} seconds. "
597-
+ $"MPF version: {pingResponse.MpfVersion}"
598-
);
599599
return pingResponse;
600600
}
601601

@@ -606,9 +606,25 @@ CancellationToken ct
606606
await pingTask;
607607
}
608608
catch (OperationCanceledException) { }
609-
throw new TimeoutException(
610-
$"Timed out trying to connect to MPF after {_connectTimeout} seconds."
611-
);
609+
610+
ct.ThrowIfCancellationRequested();
611+
612+
try
613+
{
614+
// Try one last time in case the application lost focus and wasn't actually pinging
615+
var client = new MpfHardwareService.MpfHardwareServiceClient(channel);
616+
return await client.PingAsync(
617+
new EmptyRequest(),
618+
deadline: DateTime.UtcNow.AddSeconds(1),
619+
cancellationToken: ct
620+
);
621+
}
622+
catch (Exception ex) when (ex is IOException or RpcException)
623+
{
624+
throw new TimeoutException(
625+
$"Timed out trying to connect to MPF after {_connectTimeout} seconds."
626+
);
627+
}
612628
}
613629

614630
private async Task<PingResponse> PingUntilResponse(
@@ -617,17 +633,16 @@ CancellationToken ct
617633
)
618634
{
619635
ct.ThrowIfCancellationRequested();
636+
var client = new MpfHardwareService.MpfHardwareServiceClient(channel);
620637
while (true)
621638
{
622639
try
623640
{
624-
var client = new MpfHardwareService.MpfHardwareServiceClient(channel);
625-
var response = await client.PingAsync(
641+
return await client.PingAsync(
626642
new EmptyRequest(),
627643
deadline: DateTime.UtcNow.AddSeconds(1),
628644
cancellationToken: ct
629645
);
630-
return response;
631646
}
632647
catch (Exception ex) when (ex is IOException or RpcException)
633648
{

0 commit comments

Comments
 (0)