您可以通过清除 Windows 消息队列来跳过待处理的点击
应用程序.DoEvents();
我们使用以下自定义事件类来解决您的问题(防止多次点击并在必要时显示等待光标):
using System;
using System.Windows.Forms;
public sealed class Event {
bool forwarding;
public event EventHandler Action;
void Forward (object o, EventArgs a) {
if ((Action != null) && (!forwarding)) {
forwarding = true;
Cursor cursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
Action(o, a);
} finally {
Cursor.Current = cursor;
Application.DoEvents();
forwarding = false;
}
}
}
public EventHandler Handler {
get {
return new EventHandler(Forward);
}
}
}
您可以验证它是否适用于以下示例(仅当 HandleClick 已终止时,控制台才会输出点击):
using System;
using System.Threading;
using System.Windows.Forms;
class Program {
static void HandleClick (object o, EventArgs a) {
Console.WriteLine("Click");
Thread.Sleep(1000);
}
static void Main () {
Form f = new Form();
Button b = new Button();
//b.Click += new EventHandler(HandleClick);
Event e = new Event();
e.Action += new EventHandler(HandleClick);
b.Click += e.Handler;
f.Controls.Add(b);
Application.Run(f);
}
}
要重现您的问题,请按如下方式更改上述代码(控制台输出所有点击,有延迟):
b.Click += new EventHandler(HandleClick);
//Event e = new Event();
//e.Action += new EventHandler(HandleClick);
//b.Click += e.Handler;
Event 类可用于每个公开 EventHandler 事件(Button、MenuItem、ListView、...)的控件。
问候,
坦伯格