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.
This commit is contained in:
rbalsleyMSFT
2025-07-22 13:54:41 -07:00
parent 0e53e43c77
commit 1b80d008ef
+34
View File
@@ -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