GUI is painfully slow

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
JProvencher
Posts: 13
Last visit: Wed May 03, 2023 11:49 am

GUI is painfully slow

Post by JProvencher »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2021 5.8.195
Operating system: Windows 10 Enterprise v. 1909 Build 18363.1854
PowerShell version(s): 5.1

This was my first attempt at building a GUI with PowerShell Studio. I converted our script used to build the monthly patching schedule from multiple sources into a GUI. It looks good, but runs at least twice as slow as the console version.

I tried the execution with both an executable, and with an exported PowerShell script. I'm guessing I could use Visual Studio, but even though I use it at work, I paid for PSStudio out of my own pocket and don't feel the need to learn a different package to accomplish the same thing.

It just seems to me that there shouldn't be that much slowdown. Does running a GUI to process, say, 9000+ records require a monster machine?

It reads in two files used for cross-checking information and the previous month's schedule. The console version will take generally less than an hour. I killed the GUI when it went past two hours.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: GUI is painfully slow

Post by Alexander Riedel »

PowerShell is PowerShell, a GUI process is neither significantly faster nor significantly slower than a CLI process.
Unless of course you do things differently. Without knowing what exactly it is you are doing, it is hard to tell what is causing a delay.

Generally, a GUI can cause a slow down for a variety of reasons. Virtually all of them are architectural and not language dependent. The same mistakes would cause the same slowdown if you'd use C# or C++.
Here are some general pointers you may want to consider.

- Do not process excessive amounts of data in an event handler. It either blocks GUI updates or you need to call DoEvents at each iteration, which will slow everything down
- Use jobs to process large amounts of data. Use the jobtracker snippet to keep tabs on your jobs and update the GUI as you go along. Using jobs distributes processing to parallel threads or processes.
- Consider splitting the UI and processing into two different applications. Use the UI to start a hidden processing CLI application and display output logs on a timer basis.
- If you process data on a timer event, making the timer events too fast can cause the event queue to be overrun. Making them too slow causes unnecessary delays. Fine tune your timer intervals and keep in mind
that different machines operate at different speeds.
- Do not assume that Write-Output on a console has the same speed as outputting to a text box. Updating a text box, specially if scrolling is involved, is causing significant delays. A text box, even though
frequently used for logging output, is not the best control for that purpose. Using a list box is more efficient and performant.
- Avoid excessive output. Updating a GUI with huge amounts of text is more time consuming than scrolling write-output in a CLI. Eliminate excessive output by updating a single record counter control or a
progress bar rather than just dumping your previous write-output into a control of some kind.
- Use a profiler to examine CPU load and memory usage of your process. Using excessive memory can slow down a process and the machine. Splitting processing into parts where possible can reduce memory use.

Hope this helps.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
JProvencher
Posts: 13
Last visit: Wed May 03, 2023 11:49 am

Re: GUI is painfully slow

Post by JProvencher »

Well, that is a lot. I'm not a programmer, per se, and have only done console scripting until now.

This actually started from a script/module that someone else wrote. Looking at it, sometimes I wonder why he did things the way he did - there may be more efficient means. I don't know if any of it was due to the PowerShell version at the time of writing? Anyway...

What does everything is tied to the button that kicks it all off. What you say about "Do not process excessive amounts of data in an event handler." - I'm taking that to mean that the code that "does the work" needs to be moved to another function/scriptblock?

I'm not sure if jobs would come into play. I've used jobs before for multi-threading, but it's always been a 'Foreach $Computer' type, not records in a .csv file/variable. Though, I guess the concept is the same.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: GUI is painfully slow

Post by Alexander Riedel »

Yes, generally a button press, or any event really, should only kick off a long operation rather than performing it itself.
Jobs and jobtracker (https://www.sapien.com/blog/2019/05/01/ ... jobtracker)
are a good way of doing it. If you already have an existing, working and debugged command line app, I would consider leaving it as is and adding a GUI to sit on top that controls, monitors and displays progress.
I will move the thread to the PowerShell GUI section. Maybe someone else has additional input.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
JProvencher
Posts: 13
Last visit: Wed May 03, 2023 11:49 am

Re: GUI is painfully slow

Post by JProvencher »

Thank you. As soon as I have some time, I will split everything out to another function/script.

How is progress shown for things happening in a CLI scipt?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI is painfully slow

Post by jvierra »

JProvencher wrote: Tue Nov 02, 2021 10:56 am
How is progress shown for things happening in a CLI scipt?
Please read teh whole link posted by Alex. It will answer your question and help you understand more about how GUIs work and how to use jobs to process in teh background.
User avatar
JProvencher
Posts: 13
Last visit: Wed May 03, 2023 11:49 am

Re: GUI is painfully slow

Post by JProvencher »

One last? question. The code that "does the work", is moved out of the event block. Does it matter at all if it is in a function under Mainform.prf or Globals.ps1?
HueyHuey1
Posts: 3
Last visit: Thu Dec 16, 2021 7:00 am

Re: GUI is painfully slow

Post by HueyHuey1 »

Hey! I am also asking this question now. I think Globals.ps1
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI is painfully slow

Post by jvierra »

Please read and try to understand how to use the Job Tracker custom control set. Your questions have nothing to do with the issue. No matter where you put the code in the form it will not work. The code must be run in a job. TO do that you will have to learn a bit more about PowerShell and forms.

Here is a free book that will solve your lack of experience with coding with PowerShell and forms.
https://www.sapien.com/books_training/W ... werShell-4
User avatar
JProvencher
Posts: 13
Last visit: Wed May 03, 2023 11:49 am

Re: GUI is painfully slow

Post by JProvencher »

Thank you for your condescension. I've been coding in PowerShell for a few years now but have never worked with GUIs.

Alex had said that moving it outside the event handler would help.

Your curt reply was very much less than helpful.
This topic is 2 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked