5

I've got code in a button click like so:

try
{
    Cursor = Cursors.WaitCursor;
    GenerateReports();
}
finally
{
    Cursor = Cursors.Default;
    GC.Collect();
    GenPacketBtn.Enabled = true;
}

Nowhere else but in the finally block is the cursor set back to default, yet it does "get tired" and revert to its default state for some reason. Why would this be, and how can I assure that it will not stop "waiting" until the big daddy of all the processes (GenerateReports()) has completed?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Is GenerateReports asynchronous? – Jonathan Carroll Dec 09 '15 at 20:00
  • No, I've got no asynch code. – B. Clay Shannon-B. Crow Raven Dec 09 '15 at 20:01
  • There's no reason that it would "stop waiting" for GenerateReports() to complete. Can we see what's in that function? – Jonathan Carroll Dec 09 '15 at 20:05
  • 1
    I haven't played with Windows Forms in many, many years, and barely cursors even then, but shot in the dark, are you sure this is a matter of time? It couldn't be an issue with a control that isn't inheriting the cursor as expected, or anything like that? Aside from that, if it is time, is it always the same amount of time, roughly? And if so, is that time extended if you put a `Thread.Sleep` in before your `GenerateReports()` call? – Matthew Haugen Dec 09 '15 at 20:20
  • 1
    Its been a long time since I've worked with Winforms but I remember having to pepper long running and blocking calls with Application.DoEvents() to give the UI some time to update/repaint https://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx – Paul Dec 11 '15 at 11:12

1 Answers1

3

Use instead Control.UseWaitCursor = true, this does not time out.

If an expensive operation is being executed then Windows will take over and it will change the Cursor.WaitCursor to whatever it deems necessary. So with Cursor.WaitCursor it will either due to a timeout (but not fully sure about this) or because of Windows simply claiming ownership of the cursor without regards to its previous state. We also had a similar situation with where the Cursor did not behave as expected when performing an expensive task that involved called 3rd party PDF converters but we did not investigate more on the nature of the issue as it was not a priority.

After a bit of reading, it turned out setting the Hourglass cursor is a bit more complicated than it seems:

.net WaitCursor: how hard can it be to show an hourglass?

Also as a side note: you should use Cursor.Current = Cursors.WaitCursor as this forces the cursor to change to busy immediately , more details at : https://stackoverflow.com/a/302865/1463733

Community
  • 1
  • 1
Ahmad
  • 1,462
  • 15
  • 23