3

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

4

1 回答 1

1

问题是进程在同一个线程中运行,因此在进程开始运行之前表单实际上并没有被禁用。最简单的做法是使用 Application.DoEvents() 强制它在启动进程之前将所有内容设置为禁用,但更专业(并且可能更安全)的方法是在另一个线程中运行耗时的进程。

注意:在我自己的编程中遇到另一个故障后,我发现您可能必须在再次启用所有内容之前运行 Application.DoEvents() - 它会触发用户在禁用控件上所做的任何点击,而不是等待进程完成 - 启用控件 - 然后触发点击。

显然 DoEvents 很乱,我应该使用线程。

于 2013-07-30T22:11:09.677 回答