Skip to content

Instantly share code, notes, and snippets.

@OtterKring
Created February 6, 2025 15:55
Show Gist options
  • Save OtterKring/4ad779a6b004c425e82a52d2333c7547 to your computer and use it in GitHub Desktop.
Save OtterKring/4ad779a6b004c425e82a52d2333c7547 to your computer and use it in GitHub Desktop.
Experimenting with referenced classes in foreach-object -parallel
#region WORKING_1
class Log {
hidden [System.Diagnostics.Stopwatch] $Timer = [System.Diagnostics.Stopwatch]::New()
Log() {
$this.Timer.Start()
}
[string] Write ( [string] $msg ) {
return ( "[{0:d3}:{1:d2}:{2:d3}] {3}" -f $this.Timer.Elapsed.Minutes,$this.Timer.Elapsed.Seconds,$this.Timer.Elapsed.Milliseconds, $msg )
}
}
$log = [Log]::new()
1..4 | Foreach-Object -Parallel {
$l = $using:log
$l.Write("This is a message from thread $_")
}
#endregion WORKING_1
#region WORKING_2
class Log1 {
hidden [System.Diagnostics.Stopwatch] $Timer = [System.Diagnostics.Stopwatch]::New()
Log1() {
$this.Timer.Start()
}
[string] Write ( [string] $msg ) {
return ( "[{0:d3}:{1:d2}:{2:d3}] {3}" -f $this.Timer.Elapsed.Minutes,$this.Timer.Elapsed.Seconds,$this.Timer.Elapsed.Milliseconds, $msg )
}
[void] WriteVerbose ( [string] $msg ) {
Write-Verbose ( "[{0:d3}:{1:d2}:{2:d3}] {3}" -f $this.Timer.Elapsed.Minutes,$this.Timer.Elapsed.Seconds,$this.Timer.Elapsed.Milliseconds, $msg ) -Verbose
}
[void] WriteWarning ( [string] $msg ) {
Write-Warning ( "[{0:d3}:{1:d2}:{2:d3}] {3}" -f $this.Timer.Elapsed.Minutes,$this.Timer.Elapsed.Seconds,$this.Timer.Elapsed.Milliseconds, $msg )
}
# WRITE-ERROR DOES NOT WORK WHEN THE CLASS IS REFERENCED IN FOREACH-OBJECT PARALLEL
# [void] WriteError ( [string] $msg ) {
# Write-Error ( "[{0:d3}:{1:d2}:{2:d3}] {3}" -f $this.Timer.Elapsed.Minutes,$this.Timer.Elapsed.Seconds,$this.Timer.Elapsed.Milliseconds, $msg )
# }
}
$Log = [Log1]::new()
$Log.Write( "This is a message" )
$Log.WriteVerbose( "This is a verbose message" )
$Log.WriteWarning( "This is a warning message" )
1..4 | ForEach-Object -Parallel {
$tLog = $using:Log
$tLog.Write( "This is a message from thread $_" )
$tLog.WriteVerbose( "This is a verbose message from thread $_" )
$tLog.WriteWarning( "This is a warning message from thread $_" )
}
#endregion WORKING_2
#region ERROR
# CALLING CLASS-OWNED METHODS FROM WITHIN THE CLASS DOES NOT WORK IN FOREACH-OBJECT -PARALLEL
class Log2 {
hidden [datetime] $StartTime = [datetime]::Now
[string] Write ( [string] $msg ) {
return $this.buildMessage( $msg )
}
[void] WriteVerbose ( [string] $msg ) {
Write-Verbose $this.buildMessage( $msg ) -Verbose
}
[void] WriteWarning ( [string] $msg ) {
Write-Warning $this.buildMessage( $msg )
}
hidden [string] buildMessage ( [string] $msg ) {
return ( "[{0}] {1}" -f $this.buildTimestamp(), $msg )
}
hidden [string] buildTimestamp () {
$elapsed = [datetime]::Now - $this.StartTime
return "{0:d3}:{1:d2}:{2:d3}" -f $elapsed.Minutes,$elapsed.Seconds,$elapsed.Milliseconds
}
}
$Log = [Log2]::new()
$Log.Write( "This is a message" )
$Log.WriteVerbose( "This is a verbose message" )
$Log.WriteWarning( "This is a warning message" )
1..4 | ForEach-Object -Parallel {
$tLog = $using:Log
$tLog.Write( "This is a message from thread $_" )
$tLog.WriteVerbose( "This is a verbose message from thread $_" )
$tLog.WriteWarning( "This is a warning message from thread $_" )
}
#endregion ERROR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment