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.