Trying to determine FPS in a C # WPF program

I am writing a touch table application using WPF and C #. (And I'm not very familiar with WPF.) We suspect that we are not getting the frame rate that we โ€œrequestโ€ from the API, so I'm trying to write my own FPS calculator. I know the math to calculate it based on the internal clock, I just donโ€™t know how to access the timer in the Windows / WPF API.

What library / commands do I need to get to access the timer?

+4
source share
4 answers

I think you are looking for StopWatch . Just initialize it and reset with each start of your iteration. At the end of the iteration, do your calculation.

+2
source

Although you can use DispatcherTimer (which passes its ticks to the ui thread, causing relativity problems) or System.Threading.Timer (which can throw an exception if you try to touch any user interface controls), I would recommend using WPF profiling tools : )

+5
source

First of all, did you know that Microsoft provides a free diagnostic tool that will tell you the frame rate with which WPF refreshes the screen? I think if you are not sure that you get the frame rate that you are asking for, then maybe you can not trust her, but I found her a reliable tool. It is called Perforator and is part of the WPF Performance Suite, which you can get by following the instructions here: http://msdn.microsoft.com/library/aa969767

This is probably easier than writing your own.

Also, how exactly do you request frame rates? Which API are you using? Are you using the Timeline DesiredFrameRate property? If so, it is more often used to reduce the frame rate than to increase it. (The documents also talk about increasing the frame rate to avoid tearing, but it doesnโ€™t make much sense - tearing is caused by presenting frames from synchronization with the monitor and is not an artifact of the slow frame rate. In any case, on Vista or Windows 7, you wonโ€™t break when DWM is enabled.) This is just a hint, and WPF does not promise to match the suggested frame rate.

As for the measurement method, you can go in several ways. If you are just trying to find out if the frame rate is in the right step, you can simply increase the counter once per frame (which you usually do in the event handler for CompositionTarget.Rendering ) and set DispatcherTimer to fire once per second and show it the value in user interface, and then reset the counter. This will be somewhat rude and ready, since DispatcherTimer not entirely accurate, but it will show you if you have 15 frames per second if you expect, for example, 30 frames per second.

If you are trying to get a more accurate look (for example, you want to try to find out if frames are constantly displayed, or from time to time you lose frames), then this gets a bit more complex. But I'll wait to see if Perforator does the trick for you before making more suggestions.

+3
source

You want to either wrap the win32 synchronization calls that you usually call (e.g. QueryPerformanceCounter ) using p / Invoke, or use something in .NET that already wraps them.

You can use DateTime.Ticks , but this is probably not a high enough resolution. The Stopwatch class uses a QueryPerformanceCounter under covers.

If you want something to be reused for many systems, rather than simple diagnostics, you should warn about processor-related problems with the QPC and stopwatch. See This Question: Can the .NET Stopwatch Class Be This Scary?

0
source

All Articles