PowerShell form Show(), ShowDialog() and System.Windows.Forms.ProgressBar

3.9k views Asked by At

I have a PowerShell script that monitors progress using the System.Windows.Forms.ProgressBar control. I basically:

  • Show the form
  • Do some looping until a file exists
  • Close the form

Because of the sequencing of events (i.e, I need to show the form and then do some processing afterwards) I cannot use ShowDialog() to show my form since it halts execution. To circumvent this I use just Show().

However, if I use Show() I see the progress bar grey background, but I don't see the actual progress bar itself! If I use ShowDialog() I see all of the progress bar, but obviously my script logic won't execute.

As an example, change Show() for ShowDialog() below:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Windows.Forms.Application]::EnableVisualStyles()

$waitForm = New-Object 'System.Windows.Forms.Form'
$waitForm.ClientSize = '502, 103'

$Progressbar = New-Object System.Windows.Forms.ProgressBar
$Progressbar.Location = New-Object System.Drawing.Point(5, 5)
$Progressbar.Width = 350
$Progressbar.Height = 15
$Progressbar.Style  = [System.Windows.Forms.ProgressBarStyle]::Continuous
$Progressbar.Value = 50
$waitForm.Controls.Add($Progressbar)

$waitForm.Show($this)

Start-Sleep -Seconds 3

$waitForm.Close()
0

There are 0 answers