Skip to content

Instantly share code, notes, and snippets.

@murshex
Created December 31, 2021 07:13
Show Gist options
  • Save murshex/d4b0f37f2a29e3921fc2ae9a210e02fd to your computer and use it in GitHub Desktop.
Save murshex/d4b0f37f2a29e3921fc2ae9a210e02fd to your computer and use it in GitHub Desktop.
Fix Windows 11 Timer Resolution
public static class WindowsHelper()
{
public static void FixTimerResolution()
{
var handle = Process.GetCurrentProcess().Handle;
var info = new PROCESS_POWER_THROTTLING_STATE
{
Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION,
ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION,
StateMask = 0
};
var infoSize = Marshal.SizeOf(info);
var infoPtr = Marshal.AllocHGlobal(infoSize);
Marshal.StructureToPtr(info, infoPtr, false);
SetProcessInformation(handle, PROCESS_INFORMATION_CLASS.ProcessPowerThrottling, infoPtr, infoSize);
Marshal.FreeHGlobal(infoPtr);
}
[DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity]
private static extern bool SetProcessInformation(IntPtr hProcess, PROCESS_INFORMATION_CLASS ProcessInformationClass, IntPtr ProcessInformation, int ProcessInformationSize);
private enum PROCESS_INFORMATION_CLASS
{
ProcessMemoryPriority,
ProcessMemoryExhaustionInfo,
ProcessAppMemoryInfo,
ProcessInPrivateInfo,
ProcessPowerThrottling,
ProcessReservedValue1,
ProcessTelemetryCoverageInfo,
ProcessProtectionLevelInfo,
ProcessLeapSecondInfo,
ProcessMachineTypeInfo,
ProcessInformationClassMax
};
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_POWER_THROTTLING_STATE
{
public int Version;
public int ControlMask;
public int StateMask;
};
private const int PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1;
private const int PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION = 0x4;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment