@@ -121,7 +121,6 @@ public State Push(CodeFile? file = null)
121
121
}
122
122
123
123
// Done
124
- CurrentState . SetBusy ( true ) ;
125
124
Stack . Push ( state ) ;
126
125
CurrentState = state ;
127
126
return state ;
@@ -526,14 +525,19 @@ public async Task SetMacroPausable(bool isPausable)
526
525
}
527
526
528
527
/// <summary>
529
- /// Called when the last or all files have been aborted
528
+ /// Called when the last or all files have been aborted by the firmware
530
529
/// </summary>
531
530
/// <param name="abortAll">Whether to abort all files</param>
532
- /// <param name="fromFirmware">Whether the request came from the firmware</param>
533
- public void FilesAborted ( bool abortAll , bool fromFirmware )
531
+ public void FilesAborted ( bool abortAll )
534
532
{
535
533
bool macroAborted = false ;
536
534
535
+ // If only the last macro is aborted, we may have a pending reply for e.g. M99
536
+ if ( ! abortAll )
537
+ {
538
+ ResolvePendingReplies ( ) ;
539
+ }
540
+
537
541
// Clean up the stack
538
542
Code ? startCode = null ;
539
543
while ( CurrentState . WaitingForAcknowledgement || CurrentState . File is MacroFile )
@@ -548,9 +552,9 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
548
552
{
549
553
using ( macro . Lock ( ) )
550
554
{
551
- // Resolve potential start codes when the macro file finishes
552
- if ( startCode is not null )
555
+ if ( startCode is not null && abortAll )
553
556
{
557
+ // Wait for the macro to be fully cancelled and then cancel the code that started it
554
558
_ = macro . WaitForFinishAsync ( ) . ContinueWith ( async task =>
555
559
{
556
560
try
@@ -571,14 +575,14 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
571
575
}
572
576
else if ( startCode is not null )
573
577
{
574
- // Cancel the code that started the blocking message prompt
578
+ // This is a message prompt. Cancel the code that started it
575
579
Codes . Processor . CancelCode ( startCode ) ;
576
580
startCode = null ;
577
581
}
578
582
579
583
// Pop the stack
580
584
Pop ( ) ;
581
- if ( startCode is not null )
585
+ if ( startCode is not null && abortAll )
582
586
{
583
587
_logger . Debug ( "==> Unfinished starting code: {0}" , startCode ) ;
584
588
}
@@ -593,22 +597,32 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
593
597
if ( abortAll )
594
598
{
595
599
// Cancel pending codes and requests
596
- _allFilesAborted = ! fromFirmware && ( DataTransfer . ProtocolVersion >= 3 ) ;
597
600
InvalidateRegular ( ) ;
598
601
}
599
602
else
600
603
{
601
604
// Invalidate remaining buffered codes from the last macro file
602
605
foreach ( Code bufferedCode in BufferedCodes )
603
606
{
604
- Codes . Processor . CancelCode ( bufferedCode ) ;
607
+ if ( bufferedCode != startCode )
608
+ {
609
+ Codes . Processor . CancelCode ( bufferedCode ) ;
610
+ }
605
611
}
606
612
BufferedCodes . Clear ( ) ;
607
613
BytesBuffered = 0 ;
614
+
615
+ // If only the last file was closed (e.g. from M99), carry on with the execution of the code that started it
616
+ if ( startCode is not null )
617
+ {
618
+ BytesBuffered += startCode . BinarySize ;
619
+ BufferedCodes . Insert ( 0 , startCode ) ;
620
+ _logger . Debug ( "==> Resuming unfinished starting code: {0}" , startCode ) ;
621
+ }
608
622
}
609
623
610
624
// Abort the file print if necessary
611
- if ( ( Channel == CodeChannel . File || Channel == CodeChannel . File2 ) && ( abortAll || ! macroAborted ) )
625
+ if ( ( Channel is CodeChannel . File or CodeChannel . File2 ) && ( abortAll || ! macroAborted ) )
612
626
{
613
627
using ( JobProcessor . Lock ( ) )
614
628
{
@@ -618,15 +632,11 @@ public void FilesAborted(bool abortAll, bool fromFirmware)
618
632
}
619
633
620
634
/// <summary>
621
- /// Abort the last or all files asynchronously
635
+ /// Abort all files asynchronously
622
636
/// </summary>
623
- /// <param name="abortAll">Whether to abort all files</param>
624
- /// <param name="fromFirmware">Whether the request came from the firmware</param>
625
637
/// <returns>Asynchronous task</returns>
626
- public async Task AbortFilesAsync ( bool abortAll , bool fromFirmware )
638
+ public async Task AbortAllFilesAsync ( )
627
639
{
628
- bool macroAborted = false ;
629
-
630
640
// Clean up the stack
631
641
Code ? startCode = null ;
632
642
while ( CurrentState . WaitingForAcknowledgement || CurrentState . File is MacroFile )
@@ -660,7 +670,6 @@ public async Task AbortFilesAsync(bool abortAll, bool fromFirmware)
660
670
// Abort the macro file
661
671
macro . Abort ( ) ;
662
672
}
663
- macroAborted = true ;
664
673
}
665
674
else if ( startCode is not null )
666
675
{
@@ -675,33 +684,14 @@ public async Task AbortFilesAsync(bool abortAll, bool fromFirmware)
675
684
{
676
685
_logger . Debug ( "==> Unfinished starting code: {0}" , startCode ) ;
677
686
}
678
-
679
- // Stop if only a single file is supposed to be aborted
680
- if ( ! abortAll && macroAborted )
681
- {
682
- break ;
683
- }
684
687
}
685
688
686
- if ( abortAll )
687
- {
688
- // Cancel pending codes and requests
689
- _allFilesAborted = ! fromFirmware && ( DataTransfer . ProtocolVersion >= 3 ) ;
690
- InvalidateRegular ( ) ;
691
- }
692
- else
693
- {
694
- // Invalidate remaining buffered codes from the last macro file
695
- foreach ( Code bufferedCode in BufferedCodes )
696
- {
697
- Codes . Processor . CancelCode ( bufferedCode ) ;
698
- }
699
- BufferedCodes . Clear ( ) ;
700
- BytesBuffered = 0 ;
701
- }
689
+ // Cancel pending codes and requests
690
+ _allFilesAborted = ( DataTransfer . ProtocolVersion >= 3 ) ;
691
+ InvalidateRegular ( ) ;
702
692
703
693
// Abort the job files if necessary
704
- if ( ( Channel == CodeChannel . File || Channel == CodeChannel . File2 ) && ( abortAll || ! macroAborted ) )
694
+ if ( Channel is CodeChannel . File or CodeChannel . File2 )
705
695
{
706
696
using ( await JobProcessor . LockAsync ( ) )
707
697
{
0 commit comments