C#实现微秒级延迟,cpu占用会比较高

public void MicrosecondDelay(long microseconds)
{
const long ticksPerMillisecond = 10000L;
const long ticksPerMicrosecond = ticksPerMillisecond / 1000L;

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

long targetTicks = stopwatch.ElapsedTicks + (microseconds * ticksPerMicrosecond);

while (stopwatch.ElapsedTicks < targetTicks)
{
// Busy-wait (spin-wait)
// Note: This can be CPU-intensive, use with caution
}

stopwatch.Stop();
}

posted @ 2024-10-23 08:51  奇迹之耀  阅读(17)  评论(0编辑  收藏  举报