.net 为什么是最小线程。尽管timeBeginPeriod(1),计时器间隔为15ms
下面的代码片段.net 为什么是最小线程。尽管timeBeginPeriod(1),计时器间隔为15ms,.net,timer,sleep,.net,Timer,Sleep,下面的代码片段 [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")] public static extern uint TimeBeginPeriod(uint uMilliseconds); static void Main(string[] args) { if (TimeBeginPeriod(1) != 0) Console.WriteLine("Tim
[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
public static extern uint TimeBeginPeriod(uint uMilliseconds);
static void Main(string[] args)
{
if (TimeBeginPeriod(1) != 0)
Console.WriteLine("TimeBeginPeriod failed!");
Console.WriteLine("Sleep");
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1);
Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
sw.Restart();
}
Console.WriteLine("Threading.Timer");
sw = null;
System.Threading.Timer t = null;
int n = 0;
t = new Timer(state =>
{
if (sw == null)
sw = Stopwatch.StartNew();
else
{
Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
n++;
sw.Restart();
}
if (n == 10)
t.Change(Timeout.Infinite, Timeout.Infinite);
}, null, TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(1));
Console.ReadKey();
}
根据网络,例如a,timeBeginPeriod
影响常规(.net)计时器。那么为什么我的计时器仍然具有这种粗粒度呢<代码>线程。睡眠似乎很好
可能相关:它运行在Windows 7上,64位,.net 4,在VMWare内部。评论是错误的。我的经验是多媒体计时器不会影响.NET计时器。也就是说,它不会改变它们支持的最短时间段,即大约15毫秒。这可能会提高它们的准确性。也就是说,如果你要求16毫秒,你实际上可能得到16毫秒,而不是“15到30毫秒之间的某个地方” 我不清楚为什么.NET定时器被限制在15毫秒 在公认的答案中有一些信息 如果您正在为.NET寻找更高分辨率的计时器,您可能不应该使用多媒体计时器。这些在Windows API中已被弃用。使用计时器队列计时器。见我的文章:
Sleep
0.151834939915548
0.757358826331279
0.786901687225611
0.712520725399457
0.715593741662697
0.798704863327602
0.5724889615859
0.648825479215934
0.436927039609783
0.517873081634677
Threading.Timer
15.5841035662354
14.8620145856526
15.1098812837944
14.4202684978119
15.3883384620112
14.7210748852159
15.307462261265
15.7125416777831
14.5991320125882
15.6035194417168