Created
July 29, 2019 14:23
-
-
Save scrthq/8710f93ce0292bca0a0cd0bf33b0e6f9 to your computer and use it in GitHub Desktop.
Backgrounding tasks in PowerShell classes using either the PoshRSJob and ThreadJob module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Backgrounder { | |
[string[]] $Updates | |
Backgrounder(){} | |
[void] AddUpdate([string]$update) { | |
$this.Updates += $update | |
} | |
[void] Update() { | |
1..5 | ForEach-Object { | |
# ThreadJob | |
Start-ThreadJob -Name {$_} -ArgumentList $_ -ScriptBlock { | |
Param($item) | |
# Can't use $using:this.Updates with assignment operator. Error: The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. | |
# Access by reassigning $using:this to local variable $class. | |
# Tried reassigning back to $this, but it appears to break the AddUpdate() method call usage for both ThreadJob and RSJob completely | |
$class = $using:this | |
$class.Updates += "ThreadJob via indirect assignment operator: $item" | |
# Can't use setter method either directly on $using:this. Error: Expression is not allowed in a Using expression. | |
$class.AddUpdate("ThreadJob via indirect AddUpdate method: $item") | |
} | |
# RSJob | |
Start-RSJob -Name {$_} -VariablesToImport 'this' -ArgumentList $_ -ScriptBlock { | |
Param($item) | |
# Direct usage of $this | |
$this.Updates += "RSJob via `$this assignment operator: $item" | |
$this.AddUpdate("RSJob via `$this.AddUpdate method: $item") | |
} | |
} | |
Start-Sleep 10 | |
} | |
} | |
$backgrounder = [Backgrounder]::new() | |
$backgrounder | |
$backgrounder.Update() | |
$backgrounder.Updates | |
<# OUTPUT | |
PS > $backgrounder = [Backgrounder]::new() | |
PS > $backgrounder | |
Updates | |
------- | |
PS > $backgrounder.Update() | |
PS > $backgrounder.Updates | |
ThreadJob via indirect assignment operator: 1 | |
ThreadJob via indirect AddUpdate method: 1 | |
RSJob via $this assignment operator: 1 | |
RSJob via $this.AddUpdate method: 1 | |
ThreadJob via indirect assignment operator: 2 | |
ThreadJob via indirect AddUpdate method: 2 | |
RSJob via $this assignment operator: 2 | |
RSJob via $this.AddUpdate method: 2 | |
ThreadJob via indirect assignment operator: 3 | |
ThreadJob via indirect AddUpdate method: 3 | |
RSJob via $this assignment operator: 3 | |
RSJob via $this.AddUpdate method: 3 | |
ThreadJob via indirect assignment operator: 4 | |
ThreadJob via indirect AddUpdate method: 4 | |
RSJob via $this assignment operator: 4 | |
RSJob via $this.AddUpdate method: 4 | |
ThreadJob via indirect assignment operator: 5 | |
ThreadJob via indirect AddUpdate method: 5 | |
RSJob via $this assignment operator: 5 | |
RSJob via $this.AddUpdate method: 5 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment