• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

How can I get rid of the DoEvents call needed to display a change in the WaitCursor (or can't I?)

In many places throughout my application I want to display a waitcursor, and as it is an MDI application I need to use Application.WaitCursor so that they don't try to do anything else until whatever it is the program is doing has ended.

However, a straight call to change the cursor does not work unless I call Application.DoEvents after each change of the cursor.  For example, when closing the application I want to display the wait cursor whilst local settings are written to disk:

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Check if user meant to close the application
            if (UserSettings.AppCloseQuery)
            {
                DialogResult result = MessageBox.Show("Are you sure you want to close?",
                    "My Application", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.No || result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }
            }

            Application.UseWaitCursor = true;

            Application.DoEvents(); // <-- without this, the cursor does not change

            // If a data file is currently open, close it
            if (ApplicationSettings.DataFileOpen)
            {
                CloseCurrentFile();
            }

            // Save the local settings
            SaveLocalSettings();

        }

There are hundreds of instances of this requirement, and I would like to get rid of them all if I can - anyone got any ideas?

Chris Bray
0
chrisbray
Asked:
chrisbray
  • 7
  • 5
1 Solution
 
surajgupthaCommented:
Try Cursor oldCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

instead of your current Application.UseWaitCursor = true;
0
 
chrisbrayAuthor Commented:
surajguptha:

That only sets the cursor on the current form, and not on the entire application.  That is the entire point of the question!!

If you review the original question you will see that this is an MDI application, and I need to change the cursor REGARDLESS of what form or forms it is over...

Chris Bray.
0
 
surajgupthaCommented:
There are ways to do it by writing an event on focus of every form to preserve the current cursor,I would rather just call the do events method instead of getting into that complexity unless u have some other reason not to use it.

When u set the application.cursor, it pushes a request to the message queue indicating the cursor to change. for some reason it doesnt get processed immediately, which is why u have to force it to process using do events.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chrisbrayAuthor Commented:
surajguptha:

Me too!  However, it does mean LOADS of DoEvents calls all over the place which is not good on many levels.

I was hoping an expert could find a way around it...

Chris Bray
0
 
chrisbrayAuthor Commented:
I suppose I could just write a method that sets Application.WaitCursor and calls DoEvents, and call that instead...

Chris Bray.
0
 
surajgupthaCommented:
Well even if it is packed into one method, there would still be the same number of calls to Do events.
0
 
chrisbrayAuthor Commented:
Agreed. But at least it reduces all the wasted typing, and removes hundreds of lines of code from the project.

Of course, I would much prefer a solution without the need for DoEvents at all, but if it can't be done at least by moving it to one method the code is clearer and shorter!

Chris Bray..  
0
 
surajgupthaCommented:
ok cool. Is there anything else you are looking for?
0
 
chrisbrayAuthor Commented:
No t unless you (or someone else) can provide a working answer to the original question.  I accept that the answer may be that it cannot be done, but keep hoping :-)

Chris Bray.
0
 
chrisbrayAuthor Commented:
I agree that the question should be deleted.

Chris Bray.
0
 
surajgupthaCommented:
The answer is that it cannot be done. I thought we agreed on that. If you think otherwise you can go ahead and delete the question
0
 
chrisbrayAuthor Commented:
OK.  if that is the answer I am happy to accept it and award the points to surajguptha

Chris Bray.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now