Winform跑马灯进度条冻结
我写了一个小脚本作为字幕进度条的测试。问题是进度条冻结。此外,保存在 $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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我的评论中所述,使用
Wait-Job
将阻塞您当前的线程,因此表单也将变得无响应。相反,您可以使用循环while
或do
来等待作业并调用Application.DoEvents
方法作为解决方法: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 loopwhile
ordo
, to wait for your job and callApplication.DoEvents
Method as a workaround: