我可以提高Thread.sleep代码的分辨率是多少? [英] Can I improve the resolution of Thread.Sleep?

查看:225
本文介绍了我可以提高Thread.sleep代码的分辨率是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Thread.Sleep()号决议1变化到15.6ms

鉴于这种控制台应用程序:

Given this console app:

class Program
{
    static void Main()
    {
        int outer = 100;
        int inner = 100;
        Stopwatch sw = new Stopwatch();

        for (int j = 0; j < outer; j++)
        {
            int i;
            sw.Restart();
            for (i = 0; i < inner; i++)
                Thread.Sleep(1);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }
}

我的预期输出为100号接近100相反,我得到的是这样的:

I expected the output to be 100 numbers close to 100. Instead, I get something like this:

99 99 99 100 99 99 99 106 106 99 99 99 100 100 99 99 99 99 101 99 99 99 99 99 101 99 99 99 99 101 99 99 99 100 99 99 99 99 99 103 99 99 99 99 100 99 99 99 99 813 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1560 1559 1559 1559 1559 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1559 1558 1558 1558 1558 1558

99 99 99 100 99 99 99 106 106 99 99 99 100 100 99 99 99 99 101 99 99 99 99 99 101 99 99 99 99 101 99 99 99 100 99 99 99 99 99 103 99 99 99 99 100 99 99 99 99 813 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1559 1560 1559 1559 1559 1559 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1558 1559 1558 1558 1558 1558 1558

但有时我也不会得到任何准确的结果;这将是1559〜每次。

But sometimes I won't get any accurate results; it will be ~1559 every time.

为什么不是一致的?

一些谷歌搜索告诉我,15.6ms是一个时间片的长度,这样解释了〜1559年的结果。但为什么我有时会得到正确的结果,其他时候我只是得到了15.6的倍数? (例如Thread.sleep代码(20)通常会给予〜31.2ms)

Some googling taught me that 15.6ms is the length of a timeslice, so that explains the ~1559 results. But why is it that I sometimes get the right result, and other times I just get a multiple of 15.6? (e.g. Thread.Sleep(20) will usually give ~31.2ms)

它如何影响到硬件或软件?

我问,因为这导致了我发现它的:

I ask this because of what led me to discover it:

我一直在开发我的32位双核机上的应用。今天我的机器升级到64位四核了一个完整的操作系统重新安装。 (Windows 7和.NET 4中的两种情况下,但是我不能肯定的旧机器有W7 SP1;新一一样)

I had been developing my application on a 32 bit dual-core machine. Today my machine was upgraded to 64bit quad-core with a complete OS re-install. (Windows 7 and .NET 4 in both cases, however I can't be sure the older machine had W7 SP1; the new one does.)

当我运行在新机器上的应用程序,我immediatey注意到我的表需要较长时间才能淡出。我有一个自定义的方法来淡化我的形式,它使用了Thread.Sleep(),其值从10变到50。在旧系统中,这似乎每次都正常工作。在新系统中它采取更长的时间褪色比它应该。

Upon running my application on the new machine, I immediatey notice my forms take longer to fade out. I have a custom method to fade my forms which uses Thread.Sleep() with values varying from 10 to 50. On the old system this seemed to work perfectly every time. On the new system it's taking much longer to fade than it should.

为什么我的旧体制和我的新系统之间的bahavior变化?难道这涉及到硬件,还是软件?

Why did this bahavior change between my old system and my new system? Is this related to hardware, or software?

我可以使它始终准确吗? (〜1ms的分辨率)

有什么我可以在我的计划做,使Thread.sleep()方法可靠精确到1ms左右?甚至10ms的?

Is there something I can do in my program to make Thread.Sleep() reliably accurate to about 1ms? Or even 10ms?

推荐答案

我能回答我一个问题:我可以使它始终准确吗? (〜1ms的分辨率)

I can answer one of my questions: Can I make it consistently accurate? (~1ms resolution)

是的,看来我可以使用的 timeBeginPeriod()和timeEndPeriod()。

Yes, it seems that I can, using timeBeginPeriod() and timeEndPeriod().

我测试过这一点,它的工作原理。

I've tested this and it works.

有些事情我读过建议调用timeBeginPeriod(1)应用程序的持续时间是一个坏主意。但是,调用它在很短的方法开始,然后用timeEndPeriod()的方法结束清除它应该没问题。

Some things I've read suggest that calling timeBeginPeriod(1) for the duration of the application is a bad idea. However, calling it at the start of a short method and then clearing it with timeEndPeriod() at the end of the method should be okay.

不过,我也将研究使用定时器。

Nevertheless I will also investigate using timers.

这篇关于我可以提高Thread.sleep代码的分辨率是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆