0

I have a problem building a splash screen with PowerShell. I'd like to display it while the script runs another set of instructions in the background. I was initially using ShowDialog() but I have read and witnessed that it stops the execution of the script and was told to use Show() instead.

The problem is that it shows the form for a split second and goes away instantly. I have also read that in order for this to work, I have to use multiple threads; displaying the form in main thread, while running the rest on another thread using Start-Job. But the behavior is the same. How do I make it so it shows for X duration, then closes after a Start-Sleep?

Here's my code:

$Image1Path = "C:\Logo.png"
$Image2Path = "C:\Sync.gif"

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

#Create main form
$MainForm = New-Object System.Windows.Forms.Form 
$MainForm.Controls.Add($Text1)
$MainForm.ClientSize = '830, 250'
$MainForm.ControlBox = $False
$MainForm.MaximizeBox = $False
$MainForm.MinimizeBox = $False
$MainForm.Name = "MainForm"
$MainForm.RightToLeftLayout = $True
$MainForm.StartPosition = 'CenterScreen'
$MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog

#Create text label inside main form
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = "Microsoft Sans Serif, 10.2pt, style=Bold"
$Label1.Location = '60, 110'
$Label1.Name = "Label1"
$Label1.Size = '710, 34'
$Label1.TabIndex = 0
$Label1.Text = "Some text here."
$Label1.TextAlign = 'MiddleCenter'
$MainForm.Controls.Add($Label1)

#Create picture box for Image1
$Image1 = [System.Drawing.Image]::FromFile($Image1Path)
$PictureBox1 = New-Object Windows.Forms.PictureBox
$PictureBox1.Width = $Image1.Size.Width
$PictureBox1.Height = $Image1.Size.Height
$PictureBox1.Image = $Image1
$PictureBox1.Location = '300, 20'
$MainForm.Controls.Add($PictureBox1)

#Create picture box for Image2
$Image2 = [System.Drawing.Image]::FromFile($Image2Path)
$PictureBox2 = New-Object Windows.Forms.PictureBox
$PictureBox2.Width = $Image2.Size.Width
$PictureBox2.Height = $Image2.Size.Height
$PictureBox2.Image = $Image2
$PictureBox2.Location = '390, 170'
$MainForm.Controls.Add($PictureBox2)

$MainForm.Add_Shown({$MainForm.Activate()})
$MainForm.Show()

Start-Sleep -Seconds 5

$MainForm.Close()

Any help is appreciated. Thank you.

mappr
  • 1
  • 1
  • 1
    Just had a chance to try and that code works fine for me. – Adil Hindistan Jun 06 '14 at 02:30
  • This is a kind of duplicate of [In PowerShell Form.Show() does not work right, but Form.ShowDialog() does](http://stackoverflow.com/q/5986681/608772). I gave there an explanation and a kind of turn arround, but other contributors gave some good solutions. – JPBlanc Jun 06 '14 at 03:52
  • Yep JPBlanc, I read that thread beforehand, but it didn't work so I thought I was doing something wrong. I did figure out another way in the mean time, by calling a 2nd Powershell process and stopping it after the main background job is done. – mappr Jun 07 '14 at 02:15

1 Answers1

0

Replace ShowDialog() instead of show and try again.