public static void SetRefreshRateBasedOnFpsThread()
        {
            try
            {
                if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR &&
                    !GUIGraphicsContext.ForcedRefreshRate3D)
                {
                    using (Settings xmlreader = new MPSettings())
                    {
                        if (!xmlreader.GetValueAsBool("general", "useInternalDRC", false))
                        {
                            return;
                        }
                    }
                }

                double currentRR = 0;
                if (GUIGraphicsContext.DX9Device != null && !GUIGraphicsContext.DX9Device.Disposed)
                {
                    if ((GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal == -1) ||
                        (Manager.Adapters.Count <= GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal) ||
                        (Manager.Adapters.Count > Screen.AllScreens.Length))
                    {
                        Log.Info("RefreshRateChanger.SetRefreshRateBasedOnFPS: adapter number out of bounds");
                    }
                    else
                    {
                        currentRR =
                            Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].CurrentDisplayMode.RefreshRate;
                    }
                    _refreshrateChangeCurrentRR = currentRR;

                    bool deviceReset;
                    bool forceRefreshRate;
                    using (Settings xmlreader = new MPSettings())
                    {
                        if (!xmlreader.GetValueAsBool("general", "autochangerefreshrate", false))
                        {
                            if (GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR ||
                                !GUIGraphicsContext.ForcedRefreshRate3D)
                            {
                                Log.Info("RefreshRateChanger.SetRefreshRateBasedOnFPS: 'auto refreshrate changer' disabled");
                                return;
                            }
                        }
                        forceRefreshRate = xmlreader.GetValueAsBool("general", "force_refresh_rate", false);
                        deviceReset      = xmlreader.GetValueAsBool("general", "devicereset", false);
                    }

                    double newRR;
                    string newExtCmd;
                    string newRRDescription;
                    FindExtCmdfromSettings(_workerFps, out newRR, out newExtCmd, out newRRDescription);

                    if (newRR > 0 && (currentRR != newRR || forceRefreshRate) ||
                        (GUIGraphicsContext.ForcedRefreshRate3D && !GUIGraphicsContext.ForcedRefreshRate3DDone))
                    {
                        Log.Info("RefreshRateChanger.SetRefreshRateBasedOnFPS: current refreshrate is {0}hz - changing it to {1}hz",
                                 currentRR, newRR);

                        // Add a delay for HDR
                        if (!g_Player.Playing && GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
                        {
                            Log.Debug("RefreshRateChanger.SetRefreshRateBasedOnFPS delayed start when using madVR");
                            Thread.Sleep(10000);
                        }

                        if (String.IsNullOrEmpty(newExtCmd))
                        {
                            Log.Info(
                                "RefreshRateChanger.SetRefreshRateBasedOnFPS: using internal win32 method for changing refreshrate. current is {0}hz, desired is {1}",
                                currentRR, newRR);
                            Log.Info("RefreshRateChanger AdapterOrdinal value is {0}",
                                     (uint)GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal);
                            Win32.CycleRefreshRate((uint)GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal, newRR);
                            NotifyRefreshRateChanged(newRRDescription, false);
                        }
                        else if (RunExternalJob(newExtCmd, _workerStrFile, _workerType, deviceReset) && newRR != currentRR)
                        {
                            Win32.FixDwm();
                            NotifyRefreshRateChanged(newRRDescription, false);
                        }

                        if (GUIGraphicsContext.Vmr9Active &&
                            GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.EVR)
                        {
                            Log.Info(
                                "RefreshRateChanger.SetRefreshRateBasedOnFPS: dynamic refresh rate change - notify video renderer");
                            VMR9Util.g_vmr9.UpdateEVRDisplayFPS();
                        }
                    }
                    else
                    {
                        if (newRR == 0)
                        {
                            Log.Info(
                                "RefreshRateChanger.SetRefreshRateBasedOnFPS: could not find a matching refreshrate based on {0} fps (check config)",
                                _workerFps);
                        }
                        else
                        {
                            Log.Info(
                                "RefreshRateChanger.SetRefreshRateBasedOnFPS: no refreshrate change required. current is {0}hz, desired is {1}",
                                currentRR, newRR);
                        }
                    }
                    Log.Info("RefreshRateChanger.SwitchFocus");
                    Util.Utils.SwitchFocus();
                }
            }
            catch (Exception ex)
            {
                // RefreshRate failed
                Log.Error("RefreshRate failed: {0}", ex.Message);
            }
        }