问题描述
我想在PowerShell中使用运行空间池来执行后台操作.但是我需要从主线程访问WPF窗口变量.
I wanted to use a Runspace Pool in PowerShell to perform Background actions. But I need to access the WPF Window Variable from the Main Thread.
普通运行空间可以选择:
Normal Runspaces have the option:
$runspace.SessionStateProxy.SetVariable('xamGUI',$xamGUI)
但是如何使用RunspacePool进行相同操作?
But how do I do the same with a RunspacePool?
推荐答案
将一个变量添加到运行空间池中要花点时间,但是仍然可以实现.您将需要创建一个InitialSessionState对象,然后创建一个SessionStateVariableEntry对象,其中包含要添加到运行空间池中的变量.
It is a little more involved to add a variable to a runspacepool, but still definitely doable. You will need to create an InitialSessionState object and then create a SessionStateVariableEntry object that contains the variable that you want to add to the runspacepool.
[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)
这篇关于具有运行空间池的SessionStateProxy变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!