0

I'm using Application.UseWaitCursor in a method:

    private void connectButton_Click(object sender, EventArgs e)
    {
        if (this.ValidateForm())
        {
            Application.UseWaitCursor = true;
            this.connectButton.Enabled = false;
            this.Connect();

            if (this.rememberMeCheckBox.Checked == true)
            {
                SaveCredential();
            }
            else
            {
                DeleteCredential();
            }

            Properties.Settings.Default.RememberMe = this.rememberMeCheckBox.Checked;
            Properties.Settings.Default.Save();
        }
        this.ActiveControl = this.statusLabel;
    }

... and sometimes (maybe 1 out of 20 times, it seems random) the wait cursor is delayed a bit before it shows. About 1-2 seconds, or so.

Is there any reason why it would be delayed?

Thanks for any help figuring this out.

S.A.
  • 99
  • 8
  • Any .net application (in fact Windows in general) is not a real time system - are you expecting otherwise ? – auburg Jan 11 '19 at 12:21
  • I guess I'm expecting the waiting cursor to show just when I click the button. I disable the button when I click it and it gets disabled but the waiting cursor isn't shown. – S.A. Jan 11 '19 at 12:25
  • It depends on what comes after `Application.UseWaitCursor = true;`. If it's blocking the UI thread, you won't see that cursor change until the message has been processed. – Jimi Jan 11 '19 at 12:27
  • If you're executing some 'heavy' code on the UI thread after setting this property, you indeed will notice that delay. – dymanoid Jan 11 '19 at 12:28
  • 1
    If you can do it, set `this.Cursor = Cursors.WaitCursor;` instead of setting it for all the Application Windows. It will be processed immediately. – Jimi Jan 11 '19 at 12:30
  • Thanks for the replies. I've updated the code here and I sure do some heavy code after setting the property. – S.A. Jan 11 '19 at 12:33
  • @Jimi I've tried using Cursors.WaitCursor but it doesn't work very well. It seems to automatically go away when the method is done and I'm doing some asynchronous ...stuff... in this.Connect() so the waiting cursor goes away before Connect() is completed. – S.A. Jan 11 '19 at 12:35
  • Well, then, since it's a button click event, you could add `async` to the handler and `await Task.Delay(20);` right after `Application.UseWaitCursor = true;`. This will give back control to the thread enough to process the message. I don't know why the cursor property of the Form would be reset, though. – Jimi Jan 11 '19 at 12:42
  • I tried adding async(`private async void connectButton_Click`) and await, however if I add a sleep in `Connect()` the waiting cursor is delayed. – S.A. Jan 11 '19 at 12:48
  • @Jimi in fact, I used `Cursor.Current = Cursors.WaitCursor` and it's reset. `this.Cursor = Cursors.WaitCursor` gives me a `System.StackOverflowException` ? – S.A. Jan 11 '19 at 12:51
  • You can "fix" this by calling the evil `Application.DoEvents();` immediately after the `UseWaitCursor()` call...but the symptom clearly indicates you should be moving the work in this button handler to a different thread as already suggested by the others. – Idle_Mind Jan 11 '19 at 13:38
  • I tried ...fixing... this with `Application.DoEvents()` but if I add a sleep in the `Connect()`-method the wait cursor is still delayed. – S.A. Jan 11 '19 at 13:44
  • Correct...because using `Sleep()` from anywhere within the main UI thread is a big "no, no". Move that work to a different thread. – Idle_Mind Jan 11 '19 at 13:48
  • I see. It's a bit difficult moving it to another thread though since I'm accessing a lot of form elements in the code that should be threaded. Btw. I'm completely new (as you might suspect) to C# and WinForms. – S.A. Jan 11 '19 at 13:58
  • Mind that `this.Cursor = Cursors.WaitCursor;` should not, ever, generate a `StackOverflowException`. This happens when you saturate the stack (close loops do that). There's the chance that something is wrong somewhere in the code. Find out what this is and it will most probably solve the *symptom*. – Jimi Jan 11 '19 at 14:18
  • No, that StackOverflowException seems weird. I tried with removing all code except the handler for the button click but it still gives me a StackOverflowException. D'oh! I am done working for today and going home but I'll get back to this problem on monday. Thanks for all the replies. – S.A. Jan 11 '19 at 14:32
  • @Jimi `this.Cursor = Cursors.WaitCursor;` does the trick. It's not automatically reset like `Cursor.Current = Cursors.WaitCursor;`. And I found what's causing the `StackOverflowException`. It's `MetroTextBox` from the Metroframework. When I remove all those textboxes `this.Cursor = Cursors.WaitCursor;` works. I guess I'll replace them with regular TextBoxes. – S.A. Jan 15 '19 at 11:12
  • [StackOverflowException Exception thrown on CursorChanged #43](https://github.com/dennismagno/metroframework-modern-ui/issues/43). – Jimi Jan 15 '19 at 11:28
  • 1
    @Jimi Yep. I've replaced all MetroTextBoxes now and `this.Cursor = Cursors.WaitCursor;` is working perfectly now. Thanks for all the replies, again. – S.A. Jan 15 '19 at 11:32

0 Answers0