I am trying to add a progress indicator to a form in powershell. I donβt want to use the PowerShell Write-Progress cmdlet (because when I run the script from the command line, it shows a text-based execution line, and I always need a panel based on the form / graphic).
I tried this and it seemed to work (found on the Internet):
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null $form_main = New-Object System.Windows.Forms.Form $progressBar1 = New-Object System.Windows.Forms.ProgressBar $timer1 = New-Object System.Windows.Forms.Timer $timer1_OnTick = { $progressBar1.PerformStep() } $form_main.Text = 'ProgressBar demo' $progressBar1.DataBindings.DefaultDataSourceUpdateMode = 0 $progressBar1.Step = 1 $progressBar1.Name = 'progressBar1' $form_main.Controls.Add($progressBar1) $timer1.Interval = 100 $timer1.add_tick($timer1_OnTick) $timer1.Start() $form_main.ShowDialog()| Out-Null
However, I do not want the event to update the progress bar (as $ timer1_OnTic does in the example above). I want to update it myself by making calls in my entire script, for example:
$progressBar1.PerformStep()
or
$progressBar1.Value = 10
I think I need some kind of background worker that updates the progress bar when I make calls on PerformStep () or change the value of progressBar
Calling ShowDialog stops all processing inside the script until the form is closed.
source share