Table of Contents
In some cases, when running a GUI PowerShell script, Winform is frozen then you cannot move your mouse and click on the form buttons again.
The PowerShell is generally going to be single-threaded, so anything your script does will freeze the GUI. You can try the below code snippets to see how it works.
# Load the .Net assemblies
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
# Create the form
$form = New-Object Windows.Forms.Form
$form.Text = 'www.bonguides.com'
$form.Size = New-Object System.Drawing.Size(650,400)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a DNS server:'
# Create a listbox control
$listbox = New-Object System.Windows.Forms.ListBox
$listbox.Location = New-Object System.Drawing.Point(20,30)
$listbox.Size = New-Object System.Drawing.Size(600,50)
# Add the items in the listbox
@('1.1.1.1') | ForEach-Object {$listbox.Items.Add($_)}
# Create button control
$submitButton = New-Object System.Windows.Forms.Button
$submitButton.BackColor = "Green"
$submitButton.Text = "Submit"
$submitButton.Size = New-Object System.Drawing.Point (70,30)
$submitButton.Location = New-Object System.Drawing.Point(20,100)
$submitButton.ForeColor = "#ffffff"
# Create a textbox to display the output
$textboxOutput = New-Object system.Windows.Forms.TextBox
$textboxOutput.Multiline = $true
$textboxOutput.Text = "Waiting for results..."
$textboxOutput.Size = New-Object System.Drawing.Size(600,200)
$textboxOutput.Location = New-Object System.Drawing.Point(20,140)
$textboxOutput.BackColor = "#1F1F1F"
$textboxOutput.ForeColor = 'Cyan'
# Event handler when click the button
function DNSCheck {
$textboxOutput.Text = (Test-Connection 1.1.1.1 -Count 6 | Out-String)
}
$submitButton.add_click({DNSCheck})
# Add the controls to the form then display the dialog.
$form.controls.AddRange(@($listbox,$label,$submitButton,$textboxOutput))
[void]$form.ShowDialog()
# Load the .Net assemblies
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
# Create the form
$form = New-Object Windows.Forms.Form
$form.Text = 'www.bonguides.com'
$form.Size = New-Object System.Drawing.Size(650,400)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a DNS server:'
# Create a listbox control
$listbox = New-Object System.Windows.Forms.ListBox
$listbox.Location = New-Object System.Drawing.Point(20,30)
$listbox.Size = New-Object System.Drawing.Size(600,50)
# Add the items in the listbox
@('1.1.1.1') | ForEach-Object {$listbox.Items.Add($_)}
# Create button control
$submitButton = New-Object System.Windows.Forms.Button
$submitButton.BackColor = "Green"
$submitButton.Text = "Submit"
$submitButton.Size = New-Object System.Drawing.Point (70,30)
$submitButton.Location = New-Object System.Drawing.Point(20,100)
$submitButton.ForeColor = "#ffffff"
# Create a textbox to display the output
$textboxOutput = New-Object system.Windows.Forms.TextBox
$textboxOutput.Multiline = $true
$textboxOutput.Text = "Waiting for results..."
$textboxOutput.Size = New-Object System.Drawing.Size(600,200)
$textboxOutput.Location = New-Object System.Drawing.Point(20,140)
$textboxOutput.BackColor = "#1F1F1F"
$textboxOutput.ForeColor = 'Cyan'
# Event handler when click the button
function DNSCheck {
$job = Start-Job -ScriptBlock {
$global:Result = Test-Connection 1.1.1.1 -Count 6 | Out-String
}
do { [System.Windows.Forms.Application]::DoEvents() } until ($job.State -eq "Completed")
$textboxOutput.Text = $Global:Result
}
$submitButton.add_click({DNSCheck})
# Add the controls to the form then display the dialog.
$form.controls.AddRange(@($listbox,$label,$submitButton,$textboxOutput))
[void]$form.ShowDialog()
# Event handler when click the button
function DNSCheck {
$textboxOutput.Text = (Test-Connection 1.1.1.1 -Count 6 | Out-String)
}
$submitButton.add_click({DNSCheck})
# Event handler when click the button with DoEvents
function DNSCheck {
$job = Start-Job -ScriptBlock {
$global:Result = Test-Connection 1.1.1.1 -Count 6 | Out-String
}
do { [System.Windows.Forms.Application]::DoEvents() } until ($job.State -eq "Completed")
$textboxOutput.Text = $Global:Result
}
$submitButton.add_click({DNSCheck})
Not a reader? Watch this related video tutorial:
5/5 - (1 vote)