Tag: 偏差

C#为什么定时器频率非常低?

System.Timers.Timer和System.Threading.Timer以与请求的间隔大不相同的方式触发。 例如: new System.Timers.Timer(1000d / 20); 产生一个每秒发射16次而不是20次的计时器。 为了确保没有太长的事件处理程序的副作用,我写了这个小测试程序: int[] frequencies = { 5, 10, 15, 20, 30, 50, 75, 100, 200, 500 }; // Test System.Timers.Timer foreach (int frequency in frequencies) { int count = 0; // Initialize timer System.Timers.Timer timer = new System.Timers.Timer(1000d / frequency); timer.Elapsed += delegate { Interlocked.Increment(ref count); }; // Count […]