开发者

Accurately timing user interaction in a WPF window

开发者 https://www.devze.com 2023-03-24 04:16 出处:网络
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.Diagnostic

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?


You could set up something that uses SendInput to send mouse clicks to the GUI. Something like:

Initialize manual reset event
Start stopwatch_1
Send mouse click
wait for event
Get elapsed time of stopwatch_1

In your event handler:

Get elapsed time of stopwatch_1
set event

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消