C#Thread.Sleep(1000)在某些PC上花费的时间超过1000毫秒 [英] C# Thread.Sleep(1000) takes much more than 1000ms on some pc

查看:602
本文介绍了C#Thread.Sleep(1000)在某些PC上花费的时间超过1000毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个c#程序来倒数5秒.我要做的是:

I write a c# program to count down 5 seconds. What I do is:

        new Task(() =>
        {

                try
                {
                        this.Invoke((Action)delegate()
                        {
                            label1.Text = "5"; // 4, 3, 2..etc
                        });
                    }
                    Thread.Sleep(1000);
                }
                catch
                {
                    // form could be disposed
                    break;
                }

            }

        }).Start();

这在我的PC上有效,但是,当我将程序复制到窗口图形输入板时,Thread.Sleep(1000)会持续1秒钟以上.换句话说,从5、4、3、2、1更新标签需要5秒钟以上的时间(实际上是10秒钟以上).或者,更新label1.Text ="5"等花费的时间太长了,等等?这对我来说没有意义.我不知道怎么了?

This works on my PC, however, when I copied the program to a window tablet, the Thread.Sleep(1000) gives more than 1 second. In other words, it takes more than 5 seconds (in fact more than 10 seconds) to update the label from 5,4,3,2,1. Alternatively, it takes too long to update label1.Text = "5", etc? It does not make sense to me. I wonder what is wrong?

推荐答案

Sleep将在最少上等待很长时间,然后才能再次激活该线程,但是它总是可以更长.一秒钟之后,该线程将成为合格,可以由CPU调度程序执行,并且该调度程序 可以在需要时运行它.如果它特别忙碌,并且/或者使用的调度算法没有专注于快速允许新活动的线程运行,那么可能要花一些时间.

Sleep will wait at least that long before activating the thread again, but it can always be longer than that. After the one second time has passed the thread becomes eligible to execute by the CPU scheduler, and the scheduler is able to run it whenever it wants to. If it's particularly busy, and/or is using a scheduling algorithm that doesn't focus on quickly allowing newly active threads to run, it could be some time.

这篇关于C#Thread.Sleep(1000)在某些PC上花费的时间超过1000毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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