Register-ObjectEvent for space in arraylist
I am creating a pool of spaces that will have multiple spaces triggering a stop at different times.
To keep track of this, all my whitespace ends up in the collection.
I am trying to register an object event for each one so that I can return information from the workspace to the user in the gui.
There are problems in this part:
$Global:RunspaceCollection
$Global:x = [Hashtable]::Synchronized(@{})
$RunspaceCollection = @()
[Collections.Arraylist]$Results = @()
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,5)
$RunspacePool.Open()
$action = {
Foreach($Runspace in $Global:RunspaceCollection.ToArray()) {
If ($Runspace.runspace.iscompleted){
$results.add($runspace.powershell.endinvoke($Runspace.runspace))
$runspace.powershell.dispose()
$runspacecollection.remove($Runspace)
Write-Host "I AM WORKING"
test-return
}
}
Function Start-PasswordReset($username) {
$scriptblock = {
Param($userame)
start-sleep -Seconds 10
$Result = "I have reset" + $username
$result
}
$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($username)
$Powershell.RunspacePool = $RunspacePool
[Collections.Arraylist]$Global:RunspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}
Register-ObjectEvent -InputObject $runspacecollection[0].Runspace -EventName statechanged -Action $Action
}
}
The code does not work on the register-objectevent line, currently I have hardcoding 0 for testing, but this will be the current mileage in the final version. I don't know if I made a small mistake or had a fundamental lack of understanding of how this works, I am open to both possibilities!
source to share
I see a few problems, but the main one is that the StateChanged event is an event on the RunspacePool.Powershell object inside your RunspaceCollection [index], not on the RunspaceCollection.Runspace (custom Runspace property for the PSObject you have created).
Set your workspace on a Powershell object as in:
$powershell = [PowerShell]::Create()
$powershell.RunspacePool = $runspacePool
$powershell.AddScript($scriptBlock).AddArgument($username)
$Global:runspaceCollection += @{
PowerShell = $powershell
Handle = $powershell.BeginInvoke()
}
And then declare your event as:
Register-ObjectEvent -InputObject $Global:runspaceCollection[0].PowerShell.RunspacePool -EventName StateChanged -Action $Action
After that, I don't think you intended to use the Start-Password reset function inside the Action function. Here is the complete code that worked for me:
$Global:RunspaceCollection
$Global:x = [Hashtable]::Synchronized(@{})
$RunspaceCollection = @()
[Collections.Arraylist]$Results = @()
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, 5)
$RunspacePool.Open()
$action = {
Foreach ($Runspace in $Global:RunspaceCollection.ToArray()) {
If ($Runspace.runspace.iscompleted) {
$results.add($runspace.powershell.endinvoke($Runspace.runspace))
$runspace.powershell.dispose()
$runspacecollection.remove($Runspace)
Write-Host "I AM WORKING"
test-return
}
}
}
Function Start-PasswordReset($username) {
$scriptblock = {
Param($userame)
start-sleep -Seconds 10
$Result = "I have reset" + $username
$result
}
$powershell = [PowerShell]::Create()
$powershell.RunspacePool = $runspacePool
$powershell.AddScript($scriptBlock).AddArgument($username)
$Global:runspaceCollection += @{
PowerShell = $powershell
Handle = $powershell.BeginInvoke()
}
Register-ObjectEvent -InputObject $Global:runspaceCollection[0].PowerShell.RunspacePool -EventName StateChanged -Action $Action
}
source to share