最终,只要您使用的是 Powershell 2.0,-STA 模式就足够了。原因是在 STA 模式下,默认运行空间为所有交互式命令(以及脚本)重用单个线程。Zach 在 2 月份查看的 powershell 版本的行为可能与 PowerShell 2.0 的当前 RC/RTM 不同。它可能使用了 UseNewThread 而不是当前默认的 ReUseThread:
PS> [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
Events : System.Management.Automation.PSLocalEventManager
ThreadOptions : ReuseThread
RunspaceConfiguration : System.Management.Automation.Runspaces.RunspaceConfigForSingleShell
InitialSessionState :
Version : 2.0
RunspaceStateInfo : Opened
RunspaceAvailability : Busy
ConnectionInfo :
ApartmentState : STA
InstanceId : 8d3bfae1-8b64-433d-9ab9-ce640b15f84f
SessionStateProxy : System.Management.Automation.Runspaces.SessionStateProxy
Debugger : System.Management.Automation.Debugger
所以简而言之,你在这里没问题。他所说的高级技术很可能是如何使用 ReUseThread 启动新的运行空间,这现在是多余的,因为这是 -STA 的默认线程选项。但是,您可以使用此技术在 MTA 模式下在单个线程上运行;-)
-Oisin
微软 PowerShell MVP