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()
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
})