Question

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 ...

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top