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();
}