It’s common UI courtesy to show the Wait cursor when performing a long operation that requires the user to wait. Here is how the Wait cursor appears in Windows Vista:
But developers often go about this the wrong way by setting the Cursor.Current property as follows:
Cursor.Current = Cursors.WaitCursor;
The problem with this approach is it temporarily sets the Wait cursor, but doesn’t ensure that the Wait cursor shows until the end of your operation. Other programs or controls within your program can easily reset the cursor back to the default arrow:
This may confuse the user into thinking the operation has completed when in fact it’s still running.
UseWaitCursor
A much better way to show the Wait cursor is to set the UseWaitCursor property in a form to true:
form.UseWaitCursor = true;
This shows the wait cursor for the specified Form or Control and all its child controls until you set the UseWaitCursor property to false.
If your operation blocks input not just in one window but for your entire application, you can set the Application.UseWaitCursor static property to true:
Application.UseWaitCursor = true;
Note that when you set the Application.UseWaitCursor property, it overrides any settings you made on the UseWaitCursor property for any control or form in your application.
A great tip! Simple, yet very useful.
thnx….great tip
+1
My saviour… 🙂
The “wrong way” sample code should read:
Cursor.Current = Cursors.WaitCursor;
Fixed, thanks!
Mate…. You legend.
I guess I was lucky. The “wrong way” just was not working, so I had to find the “right way”.
Thanks
Works perfectly! I am so glad I found this simple solution when I couldn’t get “wrong way” to work.
When I use:
Application.UseWaitCursor = true;
Before I start a foreach which takes a wile the cursor does not change. For each object in the foreach I do some actions
Using
Cursor.Current = Cursors.WaitCursor;
Does work
Any ideas
Danny
According to MSDN:
However, note that operations that block the UI thread will also block a cursor change. Therefore, this property should only be used when performing time-consuming operations in another thread. To change the cursor globally and immediately, see the Current property.
I want to use a custom cursor as my WaitCursor. Any idea how to override the default system WaitCursor or set Application.UseWaitCursor to False and “lock” it so the system won’t just decide to overwrite my custom cursor?
Cursor.Current = Cursors.WaitCursor;
does not work at all.
this.UseWaitCursor = true; is working.
simply useful….
implemented and fixed !!!
//Try this In a static class:
private static int mMouseDepth = 0;
public static void IncMouse()
{
if (mMouseDepth++==0)
{
System.Windows.Forms.Application.UseWaitCursor = true;
System.Windows.Forms.Application.OpenForms[0].Refresh();
}
}
public static void DecMouse()
{
if (mMouseDepth == 0) return;
if (–mMouseDepth == 0)
System.Windows.Forms.Application.UseWaitCursor = false;
}
//Put an IncMouse at begininning of procedure and a decmouse juts before exit (take care on early/conditional exits ). The counter means that nested calls to IncMouse and DecMouse will not interfere with each other (such as an inner procedure restoring cursor to default before outer procedure has finished).
Application.OpenForms[0].Refresh ensures that the mouse cursor gets repainted on outermost call to IncMouse. Could also use Application.Doevents() to do this, but be careful that user does not trigger another event [e.g. could disable form etc]. Does anyone know a better way to refresh the mouse? e.g. something like cursor.refresh. btw, I think that when some people experience that usewaitcursor does not seem to work it is because the cursor does not get repainted
p.s. should be (- – mMouseDepth==0) not -mMouseDepth.
Hi..
I am creating Desktop application.
when i am checked on checkedlistbox item at that time fire one query but problem is that record is 20 lakh..
when user click on Checkedlist item after fill the grid..
but problem is that record is 20 lakh so take a time for 10 min…..
so how can i use there image progress (gif file).
my code is :
private void Chklist_SelectedIndexChanged(object sender, EventArgs e)
{
int i;
foreach (object itemchecked in Chklist.CheckedItems)
{
i = Convert.ToInt32(Chklist.Items.IndexOf(itemchecked));
i = i + 1;
catid = catid + “,” + i.ToString();
if (catid.StartsWith(“,”) == true)
{
catid = catid.Remove(0, 1);
}
query =””;
gridView1.Columns.Clear();
obj.gridview(gridControl1, query); // here i am using method of gridview which is on class file.
}
Thanks in Advance…
great it helped me a lot.
How can we implement the UseWait Cursor in Javascript / Jquery ??
thanx for great tip
Its 2015, and this tip is still da bomb.
Hi,
I have a button in the Ribbon and clicking on it starts a very long task hence I want the cursor to change to wait. However, none of the above methods makes the cursor change to wait.
As far as I can tell all of these methods works on a form. In my case no form is being opened. How can I make the cursor change to wait in this case?
Most of the time you will do this:
UseWaitCursor = true;
//will send (usualy) the event to “message queue”.
Application.DoEvents();
//will process all events in ‘message queue’ before continue.