Are there any dangers in using Application.UseWaitCursor
to turn off and on the hourglass mouse pointer?
Asked
Active
Viewed 9,508 times
3

General Grievance
- 4,555
- 31
- 31
- 45

CJ7
- 22,579
- 65
- 193
- 321
-
See http://stackoverflow.com/questions/302663/cursor-current-vs-this-cursor-in-net-c/302865#302865 – Hans Passant Feb 29 '12 at 11:28
-
What is your response to the concern raised in the comment below your answer to that question? – CJ7 Feb 29 '12 at 11:31
-
I don't see a concern, somebody just used the class from the wrong thread. The more typical use of a wait cursor is when you *don't* use a thread. – Hans Passant Feb 29 '12 at 11:46
2 Answers
6
The "danger" is in not restoring the cursor.
You can do this with try…finally
blocks to ensure that even if an exception is thrown you restore the cursor, or clean up the syntax a bit by wrapping this functionality in a class that implements IDisposable
so that you can use using
blocks instead.
public class WaitCursor : IDisposable
{
public WaitCursor()
{
Application.UseWaitCursor = true;
}
public void Dispose()
{
Application.UseWaitCursor = false;
}
}
Usage:
using (new WaitCursor())
{
// do stuff - busy, busy, busy
} // here the cursor will be restored no matter what happened

Jay
- 56,361
- 10
- 99
- 123
3
If the application will be locked until the long running operation is completed, it will be correct to use Application.UseWaitCursor
. But if you have multiple forms where only one form will be locked, it is better to set the Cursor
property explicit on the form.
You should also remeber to put Application.UseWaitCursor = false;
in a finally
block, so you ensure that the cursor is reset in cases where an application exception is thrown.

Espen Burud
- 1,871
- 10
- 9
-
Setting the cursor on the form doesn't work, a textbox control overrides it for example. This is really the reason the property exists in the first place. – Hans Passant Feb 29 '12 at 11:26
-
Yes, that is correct, but only for enabled controls. If you have a long running process which locks the form, you also need to disable the possibility to enter data. Eg. put all controls in a panel which you can disable (Enable = false;) during the processing. – Espen Burud Feb 29 '12 at 11:38