1

I have a problem with a MessageBox window changing my wait cursor back to default and not letting me override it again. If i comment out the "Are you sure" message box the wait cursor works as intended (wait cursor is visible during the long running method). However with the program as normal no matter what i do the mouse is always default.

    private void btn_Click(object sender, EventArgs e)
    {
        this.Cursor = Cursors.WaitCursor;

        if (string.IsNullOrWhiteSpace(textbox1.Text))
        {
            MessageBox.Show("Text Box 1 Can't Be Empty");
        }
        else if (string.IsNullOrWhiteSpace(textbox3.Text))
        {
            MessageBox.Show("Text Box 3 Can't Be Empty");
        }
        else if (string.IsNullOrWhiteSpace(textbox4.Text))
        {
            MessageBox.Show("Text Box 4 can't be empty");
        }
        else if (string.IsNullOrWhiteSpace(textBox2.Text))
        {
            HelperClass.MethodThatCanHandleEmptyTextBox2(textBox1.Text, textBox3.Text, textBox4.Text));
        }
        else 
        {
            //problem line                
            DialogResult result = MessageBox.Show("Are you sure you wish to proceed?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes)
            {
               this.Cursor = Cursors.WaitCursor; //doesn't work                   

               //long running method
               HelperClass.DoMethodThatNeedsAllFields(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); //long running method

               MessageBox.Show("All done!");
            }
         }

         this.Cursor = Cursors.Default;
      }

I have tried all of these both inside and outside of the if (result == yes) condition, but to no avail:

Cursor = Cursors.WaitCursor;

Form1.ActiveForm.UseWaitCursor = true; //throws runtime exception

Cursor.Current = Cursors.WaitCursor;

Application.UseWaitCursor = true;

Application.DoEvents();

I have also tried threads AND tasks but neither of those have worked as well. Is there something I'm missing?

MrSudds
  • 123
  • 1
  • 6

2 Answers2

0

You should do this:

Application.UseWaitCursor = true;
Application.DoEvents();

This should definitely work. Do it after message boxes are all done, you don't need it before. Message box will restore it back to false.

Though, I'm pretty sure this.Cursor = Cursors.WaitCursor; has to work as well and that message box should not change it back to default. But above approach is far more common.

Sometimes, DoEvents() may be inconvenient though depending on the complexity of your UI as it could fire some background events, i.e. chat message, tcp message or whatever you have feeding your UI.

bokibeg
  • 2,081
  • 16
  • 23
  • 1
    [Use of Application.DoEvents](http://stackoverflow.com/questions/5181777/use-of-application-doevents) is a must read for anyone reading this answer. The last paragraph in bokibeg's answer is key and ideally `HelperClass.DoMethodThatNeedsAllFields` should be run in a background task/thread. – Anthony Mar 19 '15 at 18:04
  • added the former two lines right after DialogResult result = MessageBox.Show(""); wait cursor still wasn't displayed WHILE the long method was running, looks like it did change it for a second after the method was done but the button click wasn't – MrSudds Mar 19 '15 at 18:33
  • @MrSudds are you sure that `HelperClass.DoMethodThatNeedsAllFields` method doesn't somehow restore the cursor? Could you try and just put `Thread.Sleep(2000)` to see if it would work *then*? – bokibeg Mar 19 '15 at 19:42
  • 1
    @bokibeg tried Thread.Sleep(3500) and it still didn't work. the method is a simple call to a web service. It is wrapped in a "using" block but i don't think that affects the mouse/cursor – MrSudds Mar 19 '15 at 19:52
0

changed:

 this.Cursor = Cursors.WaitCursor;

to:

 Cursor.Current = Cursors.WaitCursor;
 this.Cursor = Cursor.Current;

and it finally worked!

but because I am not sure as to why this works I will gladly accept another solution that provides some explanation.

NOTE: another curious thing is that no matter the order in which you place those three items the cursor will still do what is intended

MrSudds
  • 123
  • 1
  • 6