Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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_Powershell Core - Fatal编程技术网

Powershell 如何停止并行线程

Powershell 如何停止并行线程,powershell,powershell-core,Powershell,Powershell Core,我想停止所有线程,如果其中一个线程完成。类似于“杀死所有线程” PowerShell: workflow Test{ function writeFile($c, $n){ $path = "$env:temp\workflow_$n.txt" Remove-Item $path -ea 0 while($true){ $i++ "$n-$i"|Out-

我想停止所有线程,如果其中一个线程完成。类似于“杀死所有线程”

PowerShell:

workflow Test{
    function writeFile($c, $n){
        $path = "$env:temp\workflow_$n.txt"
        Remove-Item $path -ea 0
        while($true){
            $i++
            "$n-$i"|Out-File $path -Append
            if($i -eq $c){"$n - finished";Kill-AllThreads;break}
        }
    }
    parallel {
        writeFile 5 i
        writeFile 10 k
    }
}

test

start "$env:temp\workflow_i.txt"
start "$env:temp\workflow_k.txt"
PowerShell核心:

((5,'i'), (10,'k'))|ForEach-Object -Parallel{
    $c, $n = $_
    $path = "$env:temp\workflow_$n.txt"
    Remove-Item $path -ea 0
    while($true){
        $i++
        "$n-$i"|Out-File $path -Append
        if($i -eq $c){"$n - finished";Kill-AllThreads;break}
    }
}

start "$env:temp\workflow_i.txt"
start "$env:temp\workflow_k.txt"

我希望控制台中的
I-finished
i-1..5k-1..5在文件中。

嗯,下面是一种根据输出数量终止管道的方法(powershell 7)。它创建10个随机休眠的线程,然后在收到第一个线程的输出时终止管道

1..10 | foreach -parallel { $a = random -max 10 -min 1; sleep $a; $a } | 
  select -first 1

2

这是可能的,但不太确定在并行循环中是否可能。
foreach-parallel
!=工作流
parallel
statement@MathiasR.Jessen我知道,是7号动力地狱。