我想在 PowerShell 中使用运行空间池来执行后台操作。但我需要从主线程访问 WPF 窗口变量。
普通运行空间有以下选项:
$runspace.SessionStateProxy.SetVariable('xamGUI',$xamGUI)
但是我如何对 RunspacePool 执行同样的操作呢?
将变量添加到运行空间池稍微复杂一些,但仍然绝对可行。您需要创建一个 InitialSessionState 对象,然后创建一个 SessionStateVariableEntry 对象,其中包含要添加到运行空间池的变量。
[int]$Test = 123498765
#Create the sessionstate variable entry
$Variable = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'Test',$Test,$Null
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
#Add the variable to the sessionstate
$InitialSessionState.Variables.Add($Variable)
#Create the runspacepool using the defined sessionstate variable
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,$Throttle,$InitialSessionState,$Host)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)