Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing mouse buffer in C#

I have a problem while doing this in the code-behind file of a winform :

// Waiting Cursor + disabling form
Cursor = Cursors.WaitCursor;
this.Enabled = false;

// Synchronous method
SomeWork();

// Re-enabling form
Cursor = Cursors.Default;
this.Enabled = true;

Current Behaviour

Clicking on a button for example during Somework() will execute the method associated to the button after re-enabling the form.

Expected Behaviour

I don't expect from the form to store the clicking events of the user while the form is disabled.

Question

Is there a way to empty the Clicking cache of the form (So that I'd do it before re-enabling the form) ?

IMPORTANT EDIT

A possible easy solution would be implementing the IMessageFilter interface in the code behind of the form. Disabling the left seems easy using this PreFilterMessage :

public bool PreFilterMessage(ref Message m)
{
    // Blocks all the messages relating to the left mouse button.
    return (m.Msg >= 513 && m.Msg <= 515) ;
}

But once again, disabling and re-enabling the mouse's left clicks DOES NOT EMPTY THE MOUSE BUFFER ...

like image 848
Fares Avatar asked Mar 10 '26 16:03

Fares


1 Answers

The problem is that the process is running in the same thread, so the form doesn't actually get disabled before the process starts running. The easy thing to do would be use Application.DoEvents() to force it to set everything to disabled before starting the process, but the more professional (and probably safer) method is to run the time-consuming process in another thread.

NOTE: After running into another hitch in my own programming I found that you may have to run Application.DoEvents() before enabling everything again--it will fire any clicks the user made on the disabled controls, instead of waiting for the process to complete--enabling the controls--and THEN firing the click.

Obviously DoEvents is messy and I should be using threads.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!