Support Forums

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.
User avatar
tbednarz
Posts: 17
Joined: Tue Jan 12, 2016 3:09 am

How to create a 'Wait cursor' (hourglass)

Postby tbednarz » Tue Feb 02, 2016 5:58 am

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: 661
Joined: Wed Oct 08, 2014 6:02 am

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

Postby dan.potter » Tue Feb 02, 2016 7:39 am

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
Joined: Tue Jan 12, 2016 3:09 am

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

Postby tbednarz » Tue Feb 02, 2016 9:23 am

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.
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Tue Feb 02, 2016 9:44 am

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 86 times
User avatar
dan.potter
Posts: 661
Joined: Wed Oct 08, 2014 6:02 am

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

Postby dan.potter » Tue Feb 02, 2016 10:09 am

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?
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Tue Feb 02, 2016 10:21 am

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
Joined: Tue Jan 12, 2016 3:09 am

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

Postby tbednarz » Wed Feb 03, 2016 1:56 am

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!
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Feb 03, 2016 4:03 am

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
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Feb 03, 2016 4:21 am

Here is a simple workflow demo:
workflow ptest {
Param(
$computers
)
 
foreach -parallel($computer in $computers){
gwmi Win32_BIOS -PsComputerName $computer
 
}
 
}
 
$computer=Get-Content computers.txt
ptest $computers
User avatar
tbednarz
Posts: 17
Joined: Tue Jan 12, 2016 3:09 am

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

Postby tbednarz » Wed Feb 03, 2016 12:36 pm

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!

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 5 guests