This is a Platform Dependent Wait
you can use in any project that works by suspending the Thread
for the amount of time you specify with the lowest possible resolution that can be achieved by your system, which will likely be 1ms
It won't use more CPU
than a normal Thread.Sleep
/*
*MIT License
*
*Copyright (c) 2023 S Christison
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
using System.Runtime.InteropServices;
namespace System.Threading
{
/// <summary>
/// Platform Dependent Wait
/// Accurately wait down to 1ms if your platform will allow it
/// Wont murder your CPU
public static class Delay
{
internal const string windowsMultimediaAPIString = "winmm.dll";
[DllImport(windowsMultimediaAPIString)]
internal static extern int timeBeginPeriod(int period);
[DllImport(windowsMultimediaAPIString)]
internal static extern int timeEndPeriod(int period);
[DllImport(windowsMultimediaAPIString)]
internal static extern int timeGetDevCaps(ref TimerCapabilities caps, int sizeOfTimerCaps);
internal static TimerCapabilities Capabilities;
static Delay()
{
timeGetDevCaps(ref Capabilities, Marshal.SizeOf(Capabilities));
}
/// <summary>
/// Platform Dependent Wait
/// Accurately wait down to 1ms if your platform will allow it
/// Wont murder your CPU
/// </summary>
/// <param name="delayMs"></param>
public static void Wait(int delayMs)
{
timeBeginPeriod(Capabilities.PeriodMinimum);
Thread.Sleep(delayMs);
timeEndPeriod(Capabilities.PeriodMinimum);
}
/// <summary>
/// The Min/Max supported period for the Mutlimedia Timer in milliseconds
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TimerCapabilities
{
/// <summary>Minimum supported period in milliseconds.</summary>
public int PeriodMinimum;
/// <summary>Maximum supported period in milliseconds.</summary>
public int PeriodMaximum;
}
}
}
This will work with any version of .NET
and if your platform doesn't support this then it is the same as a normal Thread.Sleep
Thread.Sleep
is not guaranteed to sleep for exactly the specified amount of time. And using it in general is a sign that your application is poorly-designed. It's not clear what problem you're trying to use it to solve. Consider updating your question with a discussion of the real problem, rather than your failed solution. – Estimate