Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Powershell中等待线程?_Powershell - Fatal编程技术网

如何在Powershell中等待线程?

如何在Powershell中等待线程?,powershell,Powershell,我创建了一个新线程 $script = { write-host "in script.." } $p = [PowerShell]::Create() $null= $p.AddScript($script).AddArgument($object) $p.BeginInvoke() 问题是应用程序执行在$script完成之前退出。我知道我可以使用sleep-until,但我希望在ForEach中运行此代码,因此在ForEach完成后,它将等待线程完成 (我需要使用Create,因为我传

我创建了一个新线程

$script = {
 write-host "in script.."
}

$p = [PowerShell]::Create()
$null= $p.AddScript($script).AddArgument($object)
$p.BeginInvoke()
问题是应用程序执行在$script完成之前退出。我知道我可以使用
sleep-until
,但我希望在ForEach中运行此代码,因此在ForEach完成后,它将等待线程完成

(我需要使用Create,因为我传递了一个对象)


在执行所有脚本之前,我可以告诉谁powershell留下来?

问题在于使用
写入主机。您无法使用该输出执行任何操作,而且在主机之间显示该输出也不可靠。您也没有在
BeginInvoke
之后捕获作业状态。以下示例将起作用:

$p = [powershell]::Create()
$null = $p.AddScript('"Testing!"')
$r = $p.BeginInvoke()

while (-not $r.IsCompleted) {
    Start-Sleep -Seconds 3
}

$runspaceResult = $p.EndInvoke($r)

这很好,但是我需要在foreach中运行这段代码。所以我只是更新我的问题。正如它基本上解释了您要完成的任务一样。另外,请看一看后台作业运行空间功能非常强大,但没有很好的文档记录。如果你想做一些简单的事情,也许工作是一种更简单的方法。