How to create a 'Wait cursor' (hourglass)

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 8 years and 1 month 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
tbednarz
Posts: 17
Last visit: Fri Apr 07, 2017 5:55 am

How to create a 'Wait cursor' (hourglass)

Post by tbednarz »

I like to create a wait cursor to let the user know, that some processing is done. I use a progress bar already, but this is not what I want. I do sometimes some WMI processing wich takes two or three seconds if it is running on a remote machine. Then I like to signal the user with an hourglass cursor / mouse pointer, that the system is working (and has not crashed).

I tried this, but it does not work:
$form1.UseWaitCursor = $true
.... <do some WMI remote processing here...>
$form1.UseWaitCursor = $false

Could anybody please give me a hint, how this can be done with powershell GUIs?

Many thanks.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: How to create a 'Wait cursor' (hourglass)

Post by dan.potter »

You need to use a separate job and monitor the job progress. Any single process is going to freeze the form until it's complete making other controls unusable.
User avatar
tbednarz
Posts: 17
Last visit: Fri Apr 07, 2017 5:55 am

Re: How to create a 'Wait cursor' (hourglass)

Post by tbednarz »

Ok, thanks. Need to read more about it. I guess the problem is that PS is single thread only. Found an interesting article (http://www.get-blog.com/?p=189) about real multi-threading, but need to read this again.... Not so simple stuff to understand.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to create a 'Wait cursor' (hourglass)

Post by jvierra »

Dan' solution is useful for many things when you need long term and full control of the UI. It is not the easiest solution to your stated problem.

You do not need to use a job for what you are trying to do. WMI does not bock the UI completely. It is designed to block the event but does not lock the thread that the visible form lives on.

GUIs have two threads. One is the UI and the other is the background code processes. The events can even be reentered which can cause issues.

Here is an example of how easy it is to set a wait cursor when calling a long running WMI method.
Attachments
Demo-WaitCursor.psf
(37.42 KiB) Downloaded 665 times
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: How to create a 'Wait cursor' (hourglass)

Post by dan.potter »

Nice.. does the [System.Windows.Forms.Application]::DoEvents() apply to the remainder of the script? Why wouldn't it be the default behavior of forms?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to create a 'Wait cursor' (hourglass)

Post by jvierra »

The request was to set the wait cursor for the user. I disagree with this use of wait cursor. The wait cursor means that the form is busy or the control is busy. Using it globally is not what it is intended for.

The answer is not about how to run a background process; it is about how t show the cursor during a blocking process.

Yes al other events are valid. Timers will still tick.

My feeling is that the progress bar is all that is needed but the user feels that the cursor needs to show that the UI is busy. The use of DoEvents to force the display is normal although the wait cursor may not be.
User avatar
tbednarz
Posts: 17
Last visit: Fri Apr 07, 2017 5:55 am

Re: How to create a 'Wait cursor' (hourglass)

Post by tbednarz »

jvierra wrote: GUIs have two threads. One is the UI and the other is the background code processes. The events can even be reentered which can cause issues.

Here is an example of how easy it is to set a wait cursor when calling a long running WMI method.
Interesting! This works as expected!

As already mentioned I use the progress bar whenever the number of processing steps is predictable, e.g. if I enumerate the installed software on 100 machines or other inventory tasks. I can even give a feedback in a statusbar which machine I am currently pinging or running a specific task on.
But I experience very different processing times for single tasks. So connecting to a remote registry to read a couple of keys takes sometimes one second and the other day it takes 20 seconds on the same machine!! What should I tell the user? They simply say its a crap application, it hangs and there is no feedback! So the hourglass is not optimal but better than nothing!

Thanks for the posted solution!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to create a 'Wait cursor' (hourglass)

Post by jvierra »

Yes - THat is understandable.  Al long as the UI is frozen then the hourglass would be normal; just be sure that the UI is frozen.  Running a job will not freeze the UI.

A better approach is to use workflows with feedback that run as a job. The output can drive a progress bar and a text or list box that display the progress messages.

A workflow can do parallel processing on multiple threads. This is all transparent to you. Windows Workflow Manager handles all of the issues of threading and runspaces and is more efficient that using multiple jobs. A workflow can even persist across multiple reboots and remote restarts.

Look up workflow checkpoints.

http://blogs.technet.com/b/heyscripting ... art-1.aspx
http://blogs.technet.com/b/heyscripting ... art-2.aspx
http://blogs.technet.com/b/heyscripting ... art-3.aspx
http://blogs.technet.com/b/heyscripting ... art-4.aspx
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to create a 'Wait cursor' (hourglass)

Post by jvierra »

Here is a simple workflow demo:
  1. workflow ptest {
  2.     Param(
  3.         $computers
  4.     )
  5.    
  6.     foreach -parallel($computer in $computers){
  7.         gwmi Win32_BIOS -PsComputerName $computer
  8.  
  9.     }
  10.    
  11. }
  12.  
  13. $computer=Get-Content computers.txt
  14. ptest  $computers
User avatar
tbednarz
Posts: 17
Last visit: Fri Apr 07, 2017 5:55 am

Re: How to create a 'Wait cursor' (hourglass)

Post by tbednarz »

Thanks very much, this is very interesting stuff, especially when it comes to 'parallelize' things. But currently my problem is, that a single call to a remote machine has VERY different execution times. I work a lot on AD where you just do one query. Sometimes the result is here instantly sometimes it takes seconds.

When you run scripts in the background or schedule them this does not matter. But when you have a GUI and you are waiting for the result it matters a lot and a workflow does not help much.

However there are many situations, where parallelizing is VERY helpful, e.g. when you query multiple computers and have to merge the results. Cool stuff that I will dive deeper in!
This topic is 8 years and 1 month 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