Ignoring Mouse Events During WaitCursor

G

Guest

In my Windows Forms application, while executing a process that takes some
time, I am changing the cursor to the hourglass by setting Cursor.Current =
Cursors.WaitCursor.

This is working just fine, except that any mouse events generated during
this wait period (such as clicking on a button, etc.), get processed once the
processing is complete. For example, while waiting for my task to complete,
I click on a button (even though the cursor is an hourglass). When the task
is complete, the button click event is executed.

The documentation for the Cursor.Current property makes it sound like the
application will stop listening for mouse events while the cursor is not set
to Cursors.Default, but this does not seem to be the case in practice.

Any advice? Is this a bug or am I doing something incorrectly?

Thanks!

-Karen
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Karen,

Changing the shape of the mouse pointer has nothing to do with processing
mouse messages. Even when the mouse pointer is set to the hourglass, the
system still accumulates events received from the input devices (not only
the mouse, but also the keyboard and whatever device you can imagine). You
can probably forcibly clear the event queue with API calls upon the
completion of the task, but I'd consider it a hack and definitely a bad
programming style.

Why don't you use asynchronous delegates or multi-threading instead? This
way, the application won't be blocked while working on the long-running task
(that is, it will be able to repaint itself, the user will be able to
minimize and restore the window etc.), and at the same time you can disable
controls and menu items which shouldn't be available until the task
completes.
 
G

Guest

Hi Dmitriy,

Thank you for your response. I guess I misunderstood the documentation for
Cursor.Current. I do plan on running my task asynchronously and will be sure
to disable any controls that should not be available while the task is
running.

Thanks again,

Karen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top