简体   繁体   中英

Powershell multithreading not working on cmd.exe

I need to run some code in powershell using multi-thread, i have tested a simple snippet and it runs fine on a powershell console. however when i try to run on a cmd.exe the code doesnt execute and no error was thrown wondering what is going on? if someone you help on this.

sample code as follow

$throttleLimit = 10

$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $iss, $Host)
$Pool.Open()

$ScriptBlock = {
param($id)
Start-Sleep -Seconds 2
Write-Host "Done processing ID $id"
[System.Console]::WriteLine("Done processing ID $id")
}

for ($x = 1; $x -le 40; $x++) {
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($x)
$powershell.RunspacePool = $Pool
$handle = $powershell.BeginInvoke()
}

my batch file code is as follow

powershell -Command .\multiT.ps1 2>&1

In the ISE, the script finishes before the output from the threads starts to show up. I added start-sleep -sec 10 to the end of the code and I get output from cmd now. For some reason the output is doubled, though (as in, I get 2 lines for each thread).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM