Multithreading Powershell-从自定义事件处理程序访问全局同步哈希表
我试图访问事件处理程序scriptblock中的Multithreading Powershell-从自定义事件处理程序访问全局同步哈希表,multithreading,powershell,event-handling,Multithreading,Powershell,Event Handling,我试图访问事件处理程序scriptblock中的$syncHash,但似乎什么也没发生。有办法吗 $syncHash = [hashtable]::Synchronized(@{}) $syncHash.PostPocess = { [string]$path = $event.messagedata ... # trying to access $syncHash here, but failed ... } Register-EngineEvent -Sou
$syncHash
,但似乎什么也没发生。有办法吗
$syncHash = [hashtable]::Synchronized(@{})
$syncHash.PostPocess = {
[string]$path = $event.messagedata
...
# trying to access $syncHash here, but failed
...
}
Register-EngineEvent -SourceIdentifier Process_Result -Action $syncHash.PostPocess
New-Event -SourceIdentifier Process_Result -MessageData $path
谢谢,您的事件脚本块无法访问您注册事件的初始PowerShell环境 一种解决方案是通过事件的MessageData处理程序传递同步哈希,下面是您对此的修订代码:
$syncHash = [hashtable]::Synchronized(@{})
$syncHash.PostPocess = {
# Your $path variable is now in the first cell of the array $event.messagedata
[string]$path = $event.messagedata[0]
...
# Should display 'True', as $event.MessageData[1] is now your initial $syncHash
echo $event.MessageData[1].IsSynchronized
...
}
Register-EngineEvent -SourceIdentifier Process_Result -Action $syncHash.PostPocess
New-Event -SourceIdentifier Process_Result -MessageData @($path, $syncHash)
对于数组,您可以使用$event
的MessageData
成员作为参数行。谢谢,Zilog80:-)我不知道我可以传递数组。我今天就去试试。看起来我们的年龄和我30多年前玩Z80 asm时的年龄一样。它可以工作:-)谢谢你的帮助!!