0

Does anyone know how to get the monitor Screen Refresh Rate (Hz) in a C# UWP app?

...that will be valid to publish to the Microsoft Store (ie, no HWNDs?).

The reason for the question is that when writing a game, it's good to base movement on elapsed time between updates. To get smooth frame timing it's useful to know how often we're drawing to the screen and so time our frame updates to be in sync with that.

For example, if you know the Refresh Rate is 120Hz then it's optimal to set a fixed game time:

TargetElapsedTime = TimeSpan.FromTicks((long)(TimeSpan.TicksPerSecond / 120L));

Which results in consistent frame timing and smooth motion.

  • Uwp provides [HdmiDisplayMode Class](https://learn.microsoft.com/en-us/uwp/api/windows.graphics.display.core.hdmidisplaymode?view=winrt-19041) to get the refresh rate information of the device. But this method is only applicable to HDMI devices. Therefore, you might solve this issue through using relevant win32 api. – dear_vv Mar 29 '21 at 09:27
  • Thanks for the reply but unfortunately – VR Game Studio Mar 30 '21 at 21:13
  • Thanks for the reply but unfortunately HdmiDisplayInformation displayInformation = HdmiDisplayInformation.GetForCurrentView(); simply returns null in every test I've run. Apparently, "the HdmiDisplayInformation Class belongs to the Windows.Graphics.Display.Core namespace and it is used to get info about a display of your content. It does not represent a device object connected via HDMI." Using interop to user32.dll can only work on systems that have user32.dll, which devices like Xbox do not support. So I seem to be back at square one again? – VR Game Studio Mar 30 '21 at 21:19
  • You could serarch **WIN32:how to get the refresh rate for a window** via chrome, you will find a sample to do this, which works for wpf app directly. For uwp app, you need to use [ICoreWindowInterop](https://learn.microsoft.com/en-us/windows/win32/api/corewindow/nn-corewindow-icorewindowinterop?redirectedfrom=MSDN) get the HWND(“handle to a window”) instead of WindowInteropHelper that wpf used. You could refer to [this](https://stackoverflow.com/questions/34935077/getting-hwnd-off-of-corewindow-object-in-uwp) to get HWND. – dear_vv Apr 01 '21 at 09:26
  • Thank you Arya, I tried but as soon as I try to use any user32.dll (the "MonitorFromWindow" in this case) then it fails or returns IntPtr.Zero. Apparently, "Please note there are no supported APIs for UWP that accept an HWND. Any API you call will fail Windows Store certification" so my question must remain, perhaps modified to, "How can I get the monitor Refresh rate in a UWP C# app *that can be published to the MS Store*". – VR Game Studio Apr 02 '21 at 06:34
  • I’m afraid you can’t get screen refresh rate in a app that published on store. Currently, uwp can’t provide such api to do this directly. Besides, some win32 apis are restricted. If you want to publish this app in the store, they cannot be verified by the Microsoft Store. So my suggestion is that if you do want this feature, please feel free to submit your feature requirement with Windows Feedback Hub app. – dear_vv Apr 02 '21 at 08:24
  • Thank you Arya, I think you're right, as bizarre as it sounds, even though there is API to get the Refresh Rate of a Mixed Reality Headset, it's not possible to get the same for a common 2D monitor, TV or laptop screen. It means I have a game on the store that asks Xbox users to tell it what Hz their TV Screen runs at! I've left feedback on the App as you suggested. – VR Game Studio Apr 04 '21 at 09:51

1 Answers1

1

Unbound the process timing using IsFixedTimeStep = false; in Game1.

The default of graphics.SynchronizeWithVerticalRetrace = true; should cap the FPS to match the refresh rate.

It should work according to the source code comments:

The first argument instructs DXGI to block until VSync, putting the application to sleep until the next VSync. This ensures we don't waste any cycles rendering frames that will never be displayed to the screen.

If you still need the actual refresh rate in Hz, calculate the average of (1000/gameTime.ElapsedTime.TotalMilliseconds) in update.

  • Thank you and it does seem intuitive but my practical testing indicates that this method also causes stuttering in UWP (see the "Test 1" code here): https://community.monogame.net/t/getting-monitor-refresh-rate-to-eradicate-jerkiness/15294/7 – VR Game Studio Jun 19 '21 at 13:58