8,731
社区成员
发帖
与我相关
我的任务
分享
DateTime lastClick = DateTime.Now;
object obj = new object();
int i = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
this.IsEnabled = false;
var t = (DateTime.Now - lastClick).TotalMilliseconds;
i++;
lastClick = DateTime.Now;
System.Diagnostics.Debug.Print(t + "," + i + ";" + DateTime.Now);
Thread.Sleep(2000);
this.IsEnabled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.IsEnabled = false;
DispatcherHelper.DoEvents();
var t = (DateTime.Now - lastClick).TotalMilliseconds;
i++;
lastClick = DateTime.Now;
System.Diagnostics.Debug.Print(t + "," + i + ";" + DateTime.Now);
Thread.Sleep(2000);
this.IsEnabled = true;
}
public static class DispatcherHelper
{
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
try { Dispatcher.PushFrame(frame); }
catch (InvalidOperationException) { }
}
private static object ExitFrames(object frame)
{
((DispatcherFrame)frame).Continue = false;
return null;
}
}