Multithreading 如何在停止时终止PowerShell脚本中启动的线程
我有以下代码来刻录所有内核上的所有CPU:Multithreading 如何在停止时终止PowerShell脚本中启动的线程,multithreading,powershell,Multithreading,Powershell,我有以下代码来刻录所有内核上的所有CPU: <# .SYNOPSIS Script similar to BurnCpu Chaos Monkey .DESCRIPTION Using the largest int32 positive integer, this script calculates the factorial to generate 100% CPU utilisation. The script fills all CPUs. Source ForEach-Par
<#
.SYNOPSIS
Script similar to BurnCpu Chaos Monkey
.DESCRIPTION
Using the largest int32 positive integer, this script calculates the factorial to generate 100% CPU utilisation.
The script fills all CPUs.
Source ForEach-Parallel: https://powertoe.wordpress.com/2012/05/03/foreach-parallel/
Source maxin out CPU of single core: https://dthomo.wordpress.com/2012/04/19/use-powershell-to-max-out-cpu-usage/
.EXAMPLE
Chaos-BurnCPU
#>.
function ForEach-Parallel {
param(
[Parameter(Mandatory=$true,position=0)]
[System.Management.Automation.ScriptBlock] $ScriptBlock,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[PSObject]$InputObject,
[Parameter(Mandatory=$false)]
[int]$MaxThreads=5
)
BEGIN {
$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$pool = [Runspacefactory]::CreateRunspacePool(1, $maxthreads, $iss, $host)
$pool.open()
$threads = @()
$ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_)`r`n" + $Scriptblock.ToString())
}
PROCESS {
$powershell = [powershell]::Create().addscript($scriptblock).addargument($InputObject)
$powershell.runspacepool=$pool
$threads+= @{
instance = $powershell
handle = $powershell.begininvoke()
}
}
END {
$notdone = $true
while ($notdone) {
$notdone = $false
for ($i=0; $i -lt $threads.count; $i++) {
$thread = $threads[$i]
if ($thread) {
if ($thread.handle.iscompleted) {
$thread.instance.endinvoke($thread.handle)
$thread.instance.dispose()
$threads[$i] = $null
}
else {
$notdone = $true
}
}
}
}
}
}
# Burn CPU of all cores.
$numberOfCores = (Get-WmiObject -Class win32_processor).NumberOfCores
(0..$numberOfCores) | ForEach-Parallel -MaxThreads $numberOfCores {
foreach ($number in 1..2147483647) {
$result = $result * $number
}
}
。
函数ForEach并行{
param(
[参数(必需=$true,位置=0)]
[System.Management.Automation.ScriptBlock]$ScriptBlock,
[参数(必需=$true,ValueFromPipeline=$true)]
[PSObject]$InputObject,
[参数(必需=$false)]
[int]$MaxThreads=5
)
开始{
$iss=[system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$pool=[Runspacefactory]::CreateRunspacePool(1,$maxthreads,$iss,$host)
$pool.open()
$threads=@()
$ScriptBlock=$ExecutionContext.InvokeCommand.NewScriptBlock(“参数(`$\`r`n'+$ScriptBlock.ToString())
}
过程{
$powershell=[powershell]::Create().addscript($scriptblock).addargument($InputObject)
$powershell.runspacepool=$pool
$threads+=@{
实例=$powershell
句柄=$powershell.begininvoke()
}
}
结束{
$notdone=$true
而($notdone){
$notdone=$false
对于($i=0;$i-lt$threads.count;$i++){
$thread=$threads[$i]
如果($thread){
if($thread.handle.iscompleted){
$thread.instance.endinvoke($thread.handle)
$thread.instance.dispose()
$threads[$i]=$null
}
否则{
$notdone=$true
}
}
}
}
}
}
#烧掉所有核心的CPU。
$numberOfCores=(获取WMIOObject-Class win32_处理器)。numberOfCores
(0..$numberOfCores)|对于每个并行-MaxThreads$numberOfCores{
foreach($1..2147483647中的数字){
$result=$result*$number
}
}
不幸的是,当脚本停止时,进程将继续运行并保持100%的CPU,直到我们终止进程。我们如何在
ForEach Parallel
函数中运行END,或者以其他方式清理线程?下面是一种稍微不同的使用运行空间的方法:
它允许您在运行空间上指定超时。它还允许您在powershell实例的输出流中保留信息。您可以让脚本将其进程ID($PID)写入详细或调试流,并使用该脚本终止进程