使用WinForms和作业时PowerShell脚本的性能

ee7vknir  于 2023-06-24  发布在  Shell
关注(0)|答案(1)|浏览(128)

我正在编写一个包含多个窗体的powershell脚本。我有一个带按钮的主窗体。如果我klick按钮第二形式打开.第二种形式是通过作业(start-job)初始化的。虽然这也可以在没有工作的情况下工作,但我仍然更喜欢这种方法,因为据我所知,它被认为是winforms的最佳实践。问题是,如果使用作业,则执行时间明显更长,因此应用程序感觉没有响应。
测量命令的输出。

  • 没有工作:作业执行时间:4.1125毫秒
  • with job:作业执行时间:1348.9928毫秒

我想找到一种方法来加快工作的执行。有没有人有一个提示,我可以改变什么,或者这只是工作的设计?
先谢谢你了

function New-SecondForm {
    param(
        $text
    )
          
    Add-Type -AssemblyName System.Windows.Forms
    $popOutForm = New-Object System.Windows.Forms.Form
    $popOutForm.Size = New-Object System.Drawing.Size(400, 300)
    $popOutForm.Text = "SecondForm"

    $enlargedTextBox = New-Object System.Windows.Forms.RichTextBox
    $enlargedTextBox.Dock = "Fill"
    $enlargedTextBox.Multiline = $true
    $enlargedTextBox.ReadOnly = $true
 
    $popOutForm.Controls.Add($enlargedTextBox)
        
    #$popOutForm.ShowDialog() 
 
}

function New-SecondFormJob {
    param(
        $text
    )
   
    $scriptBlock = {
        
        Add-Type -AssemblyName System.Windows.Forms
        $popOutForm = New-Object System.Windows.Forms.Form
        $popOutForm.Size = New-Object System.Drawing.Size(400, 300)
        $popOutForm.Text = "SecondForm"

        $enlargedTextBox = New-Object System.Windows.Forms.RichTextBox
        $enlargedTextBox.Dock = "Fill"
        $enlargedTextBox.Multiline = $true
        $enlargedTextBox.ReadOnly = $true
 
        $popOutForm.Controls.Add($enlargedTextBox)
        
        #$popOutForm.ShowDialog() 
    }

    $job = Start-Job -ScriptBlock $scriptBlock 
    Wait-Job $job | Out-Null
    Remove-Job $job
}

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.Text = "PowerShell WinForms Example"
$form.Size = New-Object System.Drawing.Size(300, 200)
$form.StartPosition = "CenterScreen"

$button = New-Object System.Windows.Forms.Button
$button.Text = "New-SecondForm"
$button.Location = New-Object System.Drawing.Point(100, 50)
$button.Size = New-Object System.Drawing.Size(100, 30)

$button2 = New-Object System.Windows.Forms.Button
$button2.Text = "New-SecondFormJob"
$button2.Location = New-Object System.Drawing.Point(100, 100)
$button2.Size = New-Object System.Drawing.Size(100, 30)

$button.Add_Click({
        $executionTime = Measure-Command { New-SecondForm } | Select-Object -ExpandProperty TotalMilliseconds
        Write-Host = "Job execution time: $executionTime milliseconds"
    })

$button2.Add_Click({
        $executionTime = Measure-Command { New-SecondFormJob } | Select-Object -ExpandProperty TotalMilliseconds
        Write-Host = "Job execution time: $executionTime milliseconds"
    })


$form.Controls.Add($button)
$form.Controls.Add($button2)
$form.ShowDialog()
wwtsj6pe

wwtsj6pe1#

而不是:

$job = Start-Job -ScriptBlock $scriptBlock
Wait-Job $job | Out-Null
Remove-Job $job

我用过:

$newPowerShell = [PowerShell]::Create().AddScript($scriptBlock)
$job = $newPowerShell.BeginInvoke()
While (-Not $job.IsCompleted) {}
$newPowerShell.EndInvoke($job)
$newPowerShell.Dispose()

结果是:

  • 没有工作:作业执行时间:4.1125毫秒
  • with job:作业执行时间:1348.9928毫秒
  • 运行空间:作业执行时间:25.1892毫秒

相关问题