相关疑难解决方法(0)

尽管timeBeginPeriod(1),为什么最小Threading.Timer间隔15ms

以下代码段

    [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();
            } …
Run Code Online (Sandbox Code Playgroud)

.net sleep timer

3
推荐指数
1
解决办法
1444
查看次数

标签 统计

.net ×1

sleep ×1

timer ×1