C# .Net is there a way to know how many monitor frames have been shown, or to show something on screen for a set number of frames?

StackOverflow https://stackoverflow.com/questions/19632478

  •  01-07-2022
  •  | 
  •  

Question

I need to show something on the screen for an exact, very small, time interval. Having to work with monitors that have fixed (and relatively slow for this task) refresh rates, it will be the limiting factor - and I will have to allow the user to set the display time in a number of "frames" (= 16,(6)ms for 60Hz monitors).

I originally considered just using precise timer (based on Stopwatch class) but problem is, there is no way (that I know of) to find out how many frames were actually shown - so if I show something for, say, 21ms, I have a ~25% chance for it to be actually shown for 2 frames (33,3ms) and ~75% chance for it to be shown for 1 frame (16,6ms), depending on how the refresh 16,6ms interval aligns with my 21ms interval.

I know this can probably be done with Vsync in directX/opengl/etc, but i'd like to stay with simple windows forms, if possible. So, is there a way to either find out how many frames have been shown since some time, or intercept an event of new frame drawn on the monitor, or manually order a frame redraw or something like that?

Was it helpful?

Solution

It is not quite clear what do you mean by "monitor frame".

If you want to measure monitor refresh rate, then there is not much to measure. It's a fixed number, which etirely depends on monitor model and display settings. You can look it up or use win api call to get it programmatically: http://msdn.microsoft.com/en-us/library/dd162611%28v=VS.85%29.aspx. Refrash rate is not affected by your software.

If you want to measure fps, then you need a timer (e.g. StopWatch) and a frame count. You can get the latter by incrementing some variable each time you refresh the screen. OnPaint method in your form is propbably one of the places where you can do that. Fps is not affected by monitor (unless you enable VSync).

If you want to measure fps excluding frames which were skipped due to slow refresh rates... Then i think its safe to assume, that this value equals to refresh rate if refresh rate is lower then fps, and it equals to fps if refresh rate is higher then fps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top