Skip to content
Menu
ScriptingNerd
  • Home
  • Quick Tips
  • GitHub
ScriptingNerd

Building Powershell GUIs that won't freeze when you use them

Posted on February 15, 2019April 20, 2022 by Patrik

A few days ago I wrote about how important I think it is that you build a GUI for your scripts if they are to be used by anoyone else.

If you have ever built a GUI using Powershell however, you know there is a problem with it that I didn't mention, that is, whenever your script is actually doing anything the GUI will freeze and windows will tell you it isn't responding. This is not good as most users will then try to close it and try again. We don't want that.

The reason this happens is because you are running your form (GUI) and logic in the same thread, something you should never do, problem is, Powershells support for multithreading is limited at best.

In the last post I also talked about my module EasyGUI and mentioned that there are features I wouldnt go in to in that post, one of those features is an easy way to use threads.

There are two ways to use threads in EasyGUI, I go through both of them below as well as a tip on how I prefer to use them.

Method 1.

Passing in a scriptblock to the thread, here I am creating it on the fly but you could also store the scriptblock in a variable and pass that to the function instead

New-Thread {
    0..60|Foreach-Object{
        $SYNC.myLabel.Text = $_
    }
}

Method 2.

You could also write the code for the thread in a separate script file and then pass that to the New-Thread function like this

New-Thread "D:\Program\Powershell\myThread.ps1"

One thing to be aware of is that the threads will start running emediatly when you create them. Often you might not want that. What I usually do is create the threads inside a scriptblock that I store in a variable, then when I need the tread to run I run that scriptblock. That would look something like this.

$myThread = {
    New-Thread {
        0...60|Foreach-Object{
            $SYNC.myLabel.Text = $_
        }
    }
}

#Now it's time to run the thread, simply type the & sign and then the variable containing the scriptblock
&$myThread

$SYNC ? Where did that come from?

Noticed that did you?

This is a variable that's automatically created when you initialize the EasyGUI module. It's a hashtable that's synced between all threads so you can communicate between them. In the examples you see that I am using $SYNC.myLabel. This is created in the GUI like this.

$SYNC.myLabel = New-Label @{
    Location = "15, 15"
}

or like this

$myLabel = New-Label @{
    Location = "15, 15"
}
$SYNC.myLabel = $myLabel

If you want to see a complete example of a GUI script with threading implemented there is an example availiable in the Easy GUI repository.

Leave a Reply Cancel reply

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

  • Patrik Johansson
    Senior System Administrator

    Working with a wide flora of systems but with a focus on Configuration Manager (MEMCM/SCCM).

    Passionate about Powershell and building tools that are easy to use for everyone.
    In my spare time I work on other coding projects as well, mainly Booksonic

    LinkedIn GitHub Blog

Recent Posts

  • How to download CMTrace from Microsoft
  • Powershell counting to $null instead of 1
  • Running ForEach in parallel on Windows Powershell 5 (and older)
  • Use Powershell to create a "fake" program in the programs and features list for indexing purposes
  • How to read the manifest of an appx package file using Powershell

Archives

  • February 2023
  • April 2022
  • November 2021
  • March 2020
  • December 2019
  • February 2019

Categories

  • Powershell
  • Quick Tips
  • Below are affiliate links, if you click on them and buy something I may earn a small amount of money without any additional cost to you. Any links found here are for products I am using myself and can vouch for.
  • InterServer
    Very cheap SSD/HDD based virtual servers (VPS). Starting at $6/month for 1TB. Use coupon code BOOKSONIC and get the first month for just 1 cent. I have recently started using intersever for personal projects and so far I am very happy with them.
  • Contabo
    Cheap HDD/SSD/NVMe based Virtual servers (VPS)
    I have used Contabo for years both for my private needs as well as to host the booksonic demo server
©2023 ScriptingNerd | Powered by WordPress & Superb Themes