From 1b80d008efd89b99101123128060564986c75e75 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Tue, 22 Jul 2025 13:54:41 -0700 Subject: [PATCH] Stops running build job when UI is closed Adds cleanup logic to the window's `Closed` event handler to properly terminate any active background build job. This prevents the build process from becoming an orphaned job if the user closes the UI while a build is in progress. The change ensures the job, its polling timer, and log streams are all stopped and disposed of correctly. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index 23b925a..7badf2c 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -368,6 +368,40 @@ $window.Add_SourceInitialized({ # Register cleanup to reclaim memory and revert LongPathsEnabled setting when the UI window closes $window.Add_Closed({ + # Stop any running build job if the window is closed + #DEBUG + Writelog "DEBUG CurrentBuildJob: $($script:uiState.Data.currentBuildJob)" + #END DEBUG + if ($null -ne $script:uiState.Data.currentBuildJob) { + WriteLog "UI closing, stopping background build job." + + # Stop the timer + if ($null -ne $script:uiState.Data.pollTimer) { + $script:uiState.Data.pollTimer.Stop() + $script:uiState.Data.pollTimer = $null + } + + # Close the log stream + if ($null -ne $script:uiState.Data.logStreamReader) { + $script:uiState.Data.logStreamReader.Close() + $script:uiState.Data.logStreamReader.Dispose() + $script:uiState.Data.logStreamReader = $null + } + + # Stop and remove the job + $jobToStop = $script:uiState.Data.currentBuildJob + $script:uiState.Data.currentBuildJob = $null # Clear it from state first + + try { + Stop-Job -Job $jobToStop + Remove-Job -Job $jobToStop + WriteLog "Background job stopped and removed." + } + catch { + WriteLog "Error stopping or removing background job: $($_.Exception.Message)" + } + } + # Revert LongPathsEnabled registry setting if it was changed by this script if ($script:uiState.Flags.originalLongPathsValue -ne 1) { # Only revert if we changed it from something other than 1