在 WPF 窗口中准确计时用户交互
我需要计算 WPF 窗口中两个 GUI 事件之间的时间间隔 - 具体来说,是 Button_Click
事件和 Canvas_MouseLeftButtonDown
事件之间的时间。我正在使用对 System.Diagnostics.Stopwatch 的调用来测量此间隔。虽然 Stopwatch 类内部非常精确(与 DateTime.Now
等较低分辨率的替代方案相反),但我假设 GUI 引发的事件之间存在相对较大的粒度,按倒数顺序排列的帧速率(但在人类尺度上仍然非常短),但是对于非常快速的点击(我能管理的最好时间显然是 50 毫秒),假设的 GUI 事件延迟可能会很大。有谁知道这个粒度是什么,只是为了建立误差线和有效数字?
I need to time the interval between two GUI events in a WPF window - specifically, the time between a Button_Click
event and a Canvas_MouseLeftButtonDown
event. I am using calls to a System.Diagnostics.Stopwatch
to measure this interval. Whilst the Stopwatch class is internally very precise (as opposed to lower resolution alternatives like DateTime.Now
) I assume that there is a relatively large granularity between events being raised by the GUI, on the order of the reciprocal of the framerate (but still very short on a human scale) however for very rapid clicks (the best I could manage was apparently 50 ms) a hypothetical GUI event delay may be significant. Does anyone know what this granularity is, simply for the sake of establishing error bars and significant figures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置使用 SendInput 的内容将鼠标点击发送到 GUI。类似于:
在您的事件处理程序中:
基本上,通过单击按钮开始测试,延迟几秒钟,以便您可以将鼠标悬停在要单击的按钮上。
读取事件处理程序中的时间将告诉您收到点击和执行处理程序之间有多少延迟。最终读取的是总的运行时间,由于事件处理程序不执行任何操作,因此应该是总开销。减去处理该事件所需的时间。
请注意,您不想在 GUI 线程上进行等待。您需要调用
SendInput
的方法在单独的线程上执行。You could set up something that uses SendInput to send mouse clicks to the GUI. Something like:
In your event handler:
Basically, start the test by clicking on a button, have it delay a few seconds so that you can hover the mouse over the button that you want to click.
Reading the time in the event handler will tell you how much latency there is between a click being received and the handler being executed. The final read is the total elapsed time which, since the event handler doesn't do anything, should be the total overhead. Minus whatever time it takes for dealing with the event.
Note that you don't want to do the wait on the GUI thread. You'll need the method that calls
SendInput
to be executed on a separate thread.