gpt4 book ai didi

powershell - 在Powershell中使用Task类

转载 作者:行者123 更新时间:2023-12-04 12:33:36 24 4
gpt4 key购买 nike

我正在尝试使用Powershell中的Task类来异步运行操作。
但是我遇到以下异常:

Id                     : 1
Exception : System.AggregateException: One or more errors occurred. ---> System.Management.Automation.PSInvalidOperationException: There is no Runspace available to
run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script
block you attempted to invoke was: Write-Host "hey!"
at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.Management.Automation.PSInvalidOperationException: There is no Runspace available to run scripts in this thread. You can
provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was:
Write-Host "hey!"
at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
at System.Threading.Tasks.Task.Execute()<---

Status : Faulted
IsCanceled : False
IsCompleted : True
CreationOptions : DenyChildAttach
AsyncState :
IsFaulted : True
AsyncWaitHandle : System.Threading.ManualResetEvent
CompletedSynchronously : False

我的代码:
$delegate = [System.Action]{ Write-Host "Test" }
[System.Threading.Tasks.Task]::Run($delegate)

最佳答案

要使PowerShell与Task一起工作,将需要大量的工作。 Task太低了,PowerShell无法直接与其一起使用。

要异步执行PowerShell操作,请使用jobs

关于powershell - 在Powershell中使用Task类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com