Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Fix PowerShell GUI Freezes when Running

January 19, 2024
in Blog, Powershell, PowerShell WinForms, PowerShell with WPF
0
ADVERTISEMENT

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)
Previous Post

Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format

Next Post

How to Change Default Directory in Windows Subsystem for Linux WSL

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory