简体   繁体   English

Powershell 中的线程

[英]Threads in Powershell

I have a basic script to generate a Windows Form.我有一个生成 Windows 窗体的基本脚本。 In this script, a button is supposed to trigger and operation.在这个脚本中,一个按钮应该被触发和操作。 In order to prevent lagging, I would like to use multiple threads.为了防止滞后,我想使用多个线程。

After reading several tutorials, I still don't seem to get the fundamentals of it.看了几个教程,好像还是没搞懂它的基本原理。 This is my function for dthe button click:这是我单击按钮的函数:

Function Click ()
{
    Get-Job | Remove-Job
    $MaximumThreads = 4
    For ($i=0;$i -lt 5; $i++)
    {
        $ScriptBlock =
        {
            Write-Host "Job started!"
        }
        While ($(Get-Job -state running).count -ge $MaximumThreads)
        {
            Write-Host "Wait"
            Start-Sleep -Seconds 1
        }
        Start-Job -ScriptBlock $ScriptBlock
    }
    Write-Host "All jobs complete!"
}

I get this output:我得到这个输出:

Wait
Wait
Wait
Wait
Wait
Wait
All jobs complete!
Cancel

What I would expect is that the first threads start piling up until the threshold is reached before the "Wait" condition would kick in. But it appears as though the Jobs are not getting started in the first place, which leads me to believe I am missing something fundamental...我期望的是,在“等待”条件开始之前,第一个线程开始堆积,直到达到阈值。但看起来 Jobs 并没有开始,这让我相信我是缺少一些基本的东西......

Any suggestions?有什么建议? Thanks!谢谢!

I think you have to move the while outside the for-loop.我认为您必须将 while 移到 for 循环之外。

Function Click()
{
    Get-Job | Remove-Job
    $MaximumThreads = 4
    For ($i=0;$i -lt 5; $i++)
    {
        $ScriptBlock =
        {
            Write-Host "Job started!"
        }
        Start-Job -ScriptBlock $ScriptBlock
    }

    While ($(Get-Job -state running).count -ge $MaximumThreads)
    {
        Write-Host "Wait"
        Start-Sleep -Seconds 1
    }

    Write-Host "All jobs complete!"
}

In your code, you are creating jobs in a loop, and inside that same loop you are waiting for them to complete in a while loop.在您的代码中,您正在循环中创建作业,并且在同一个循环中您正在等待它们在 while 循环中完成。 Only after that while loop you start the job.只有在while循环之后你才开始工作。

Try:尝试:

function Click {
    Get-Job | Remove-Job
    $MaximumThreads = 4
    # create and start the jobs
    for ($i = 0; $i -lt $MaximumThreads; $i++) {
        $ScriptBlock = { Write-Host "Job started!" }
        # Start-job outputs job information. 
        # If you don't want that, append | Out-Null
        Start-Job -ScriptBlock $ScriptBlock
    }
    # now wait for the jobs to complete.
    while ((Get-Job -State Completed).Count -lt $MaximumThreads) {
        Write-Host "Wait"
        Start-Sleep -Seconds 1
    }
    Write-Host "All jobs complete!"
}

Click

Output:输出:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
41     Job41           BackgroundJob   Running       True            localhost             Write-Host "Job start...
43     Job43           BackgroundJob   Running       True            localhost             Write-Host "Job start...
45     Job45           BackgroundJob   Running       True            localhost             Write-Host "Job start...
47     Job47           BackgroundJob   Running       True            localhost             Write-Host "Job start...
Wait
All jobs complete!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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