@@ -555,7 +555,14 @@ private async Task WaitUntilMpfReady(GrpcChannel channel, CancellationToken ct)
555
555
switch ( _startupBehavior )
556
556
{
557
557
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
+ ) ;
559
566
break ;
560
567
case MpfStartupBehavior . DelayConnection :
561
568
await Task . Delay ( TimeSpan . FromSeconds ( _connectDelay ) ) ;
@@ -576,8 +583,6 @@ private async Task<PingResponse> PingUntilResponseOrTimeout(
576
583
CancellationToken ct
577
584
)
578
585
{
579
- Logger . Info ( "Attempting to connect to MPF..." ) ;
580
- var startTime = DateTime . Now ;
581
586
using var cts = CancellationTokenSource . CreateLinkedTokenSource ( ct ) ;
582
587
var timeoutTask = Task . Delay ( TimeSpan . FromSeconds ( _connectTimeout ) , cts . Token ) ;
583
588
var pingTask = PingUntilResponse ( channel , cts . Token ) ;
@@ -591,11 +596,6 @@ CancellationToken ct
591
596
await timeoutTask ;
592
597
}
593
598
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
- ) ;
599
599
return pingResponse ;
600
600
}
601
601
@@ -606,9 +606,25 @@ CancellationToken ct
606
606
await pingTask ;
607
607
}
608
608
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
+ }
612
628
}
613
629
614
630
private async Task < PingResponse > PingUntilResponse (
@@ -617,17 +633,16 @@ CancellationToken ct
617
633
)
618
634
{
619
635
ct . ThrowIfCancellationRequested ( ) ;
636
+ var client = new MpfHardwareService . MpfHardwareServiceClient ( channel ) ;
620
637
while ( true )
621
638
{
622
639
try
623
640
{
624
- var client = new MpfHardwareService . MpfHardwareServiceClient ( channel ) ;
625
- var response = await client . PingAsync (
641
+ return await client . PingAsync (
626
642
new EmptyRequest ( ) ,
627
643
deadline : DateTime . UtcNow . AddSeconds ( 1 ) ,
628
644
cancellationToken : ct
629
645
) ;
630
- return response ;
631
646
}
632
647
catch ( Exception ex ) when ( ex is IOException or RpcException )
633
648
{
0 commit comments