Winform跑马灯进度条冻结

发布于 2025-01-16 12:18:30 字数 1341 浏览 2 评论 0原文

我写了一个小脚本作为字幕进度条的测试。问题是进度条冻结。此外,保存在 $files 变量中的文件列表不会在最后显示,而是在脚本完成后调用时显示内容。任何见解都会有所帮助。

Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.Application]::EnableVisualStyles()
$path = "C:\"

$window = New-Object Windows.Forms.Form
$window.Size = New-Object Drawing.Size @(400,200)
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$window.Text = "STARTING UP"

$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(10, 10)
$ProgressBar1.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$ProgressBar1.UseWaitCursor = $true
$ProgressBar1.Visible = $false

$button = New-Object System.Windows.Forms.Button
$button.size = New-Object drawing.size @(50,50)
$button.Location = New-Object System.Drawing.Point(20, 70)
$button.Text = "TEST"
$window.Controls.add($button)

$button.add_Click(
{write-host "ASD"
$ProgressBar1.show()
start-job -name test -ScriptBlock  {gci -File -Recurse "D:\" -ErrorAction SilentlyContinue|select Name}
Wait-job -Name test
$files = Receive-Job -Name test
$ProgressBar1.Hide()
Write-host "$files"
}
)


$window.Controls.Add($ProgressBar1)

$window.ShowDialog()

I have written a little script as a test of marquee progress bar. The issue is that progress bar freezes. Also the list of files kept in $files variable is not beeing displayed at the end, but show content when called after script completion. Any insight will be helpfull.

Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.Application]::EnableVisualStyles()
$path = "C:\"

$window = New-Object Windows.Forms.Form
$window.Size = New-Object Drawing.Size @(400,200)
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$window.Text = "STARTING UP"

$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(10, 10)
$ProgressBar1.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$ProgressBar1.UseWaitCursor = $true
$ProgressBar1.Visible = $false

$button = New-Object System.Windows.Forms.Button
$button.size = New-Object drawing.size @(50,50)
$button.Location = New-Object System.Drawing.Point(20, 70)
$button.Text = "TEST"
$window.Controls.add($button)

$button.add_Click(
{write-host "ASD"
$ProgressBar1.show()
start-job -name test -ScriptBlock  {gci -File -Recurse "D:\" -ErrorAction SilentlyContinue|select Name}
Wait-job -Name test
$files = Receive-Job -Name test
$ProgressBar1.Hide()
Write-host "$files"
}
)


$window.Controls.Add($ProgressBar1)

$window.ShowDialog()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2025-01-23 12:18:30

正如我的评论中所述,使用 Wait-Job 将阻塞您当前的线程,因此表单也将变得无响应。相反,您可以使用循环 whiledo 来等待作业并调用 Application.DoEvents 方法作为解决方法:

$button.Add_Click({
    $ProgressBar1.Show()
    $this.Enabled = $false

    $job = Start-Job -ScriptBlock {
        Get-ChildItem -File -Recurse $HOME -ErrorAction SilentlyContinue
    }

    while ($job.State -eq 'Running') {
        [System.Windows.Forms.Application]::DoEvents()
    }

    $job | Receive-Job -AutoRemoveJob -Wait | Out-Host
    $ProgressBar1.Hide()
    $this.Enabled = $true
})

As stated in my comment, the use of Wait-Job will block your current thread hence the form will also become unresponsive. Instead, you can use a loop while or do, to wait for your job and call Application.DoEvents Method as a workaround:

$button.Add_Click({
    $ProgressBar1.Show()
    $this.Enabled = $false

    $job = Start-Job -ScriptBlock {
        Get-ChildItem -File -Recurse $HOME -ErrorAction SilentlyContinue
    }

    while ($job.State -eq 'Running') {
        [System.Windows.Forms.Application]::DoEvents()
    }

    $job | Receive-Job -AutoRemoveJob -Wait | Out-Host
    $ProgressBar1.Hide()
    $this.Enabled = $true
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文