PoshCode Archive  Artifact [e0cb99de96]

Artifact e0cb99de962bc7c62e90bcbd2199e1b7734d842cd48a2e81a07076925137d37f:

  • File Boots-amp-Background-Jobs.ps1 — part of check-in [91005b80ab] at 2018-06-10 13:08:38 on branch trunk — This sample was put together with Jaykul’s help and bits and pieces were taken from the Sample.ps1 distributed with PowerBoots. It requires the current changeset of PowerBoots. Not the 0.2 beta. (user: foureight84 size: 2972)

# encoding: ascii
# api: powershell
# title: Boots & Background Jobs
# description: This sample was put together with Jaykul’s help and bits and pieces were taken from the Sample.ps1 distributed with PowerBoots. It requires the current changeset of PowerBoots. Not the 0.2 beta.
# version: 0.2
# type: module
# author: foureight84
# license: CC0
# x-poshcode-id: 2315
# x-archived: 2010-10-27T11:03:46
#
# Shows how you can invoke a long running function from an event handler using background jobs in order to maintain UI responsiveness.
#
## NOTE: This requires the current changeset download of PowerBoots (not the 0.2 beta)
Import-Module PowerBoots

# This simulates a download function, say Jaykul's Get-Webfile
# You can output current progress for a large file, or if it's an array of links then out put the current (index/length)%
# You will need to run the function as a background thread in order for it to not interfere with the UI thread (freezes UI) when called from event handler.
Function global:Start-FakeDownload {
	$global:job = Start-Job {
      $max = 50
		foreach ($i in $(1..$max)){
			sleep 1 # lock up the thread for a second at a time
			($i/$max) * 100 # return a number we can use for progress reporting
		}
	}
}

# GUI using boots. Registers controls as global variables.
$global:Window = Boots -Width 250 -Async -Passthru -Title "Progress Meter" {
	DockPanel  {
		ProgressBar -Height 25 -Name "Progress" -Dock Top | tee -var global:progress
		Statusbar { Textblock | Tee -var global:status } -Dock Bottom 
		WrapPanel { Button "Download" -Name "Download" | tee -var global:download } -horizontalalignment center
	} -LastChildFill
}

# Add event handler for the Download button.
# Runs Background job and updates Ui
$download.Add_Click({
	# Prevents download from being pressed while running ... causes overload with $timer.
	$download.IsEnabled = $false
	$download.Content = "Downloading..."
	# Get background job out and updates controls with value
	$updateblock = {
		# Notice the -Keep usage. Job result/output clears everytime you Receive-Job.
		# -Keep allows us to get the result from the background job multiple times and also serves as a marker to figure out when the job completes
		if($($job.State -eq "Running") -or $($($job.State -eq "Completed") -and $($(Receive-Job $job -Keep)[-1] -eq 100))){
			Invoke-BootsWindow $Window {
				$progress.Value = $(Receive-Job $job -Keep)[-1]
				$status.Text = "$($(Receive-Job $job)[-1])`% done"
			}
		}
		if($($job.State -eq "Completed") -and $($(Receive-Job $job) -eq $null)){
			Invoke-BootsWindow $Window {
				$status.Text = "Download Complete"
			}
			$timer.Stop()
			$download.Content = "Download"
			$download.IsEnabled = $true
		}
	}
	$timer = new-object System.Windows.Threading.DispatcherTimer
	$timer.Interval = [TimeSpan]"0:0:3"
	$timer.Add_Tick( $updateBlock )
	Start-FakeDownload 
	$timer.start()
})