看到这些函数或类都是计算一个程序执行时间的,如何把他们用于定时。就40MS吧。

解决方案 »

  1.   

    定时已经有专门的定时器了Timer,不要多此一举。
      

  2.   

    我猜你的目的是准确定时,就像Sleep(40)。但很可惜这不能做到 - Windows本身不是实时的操作系统。我们的线程随时都可能被操作系统挂起,等操作系统再把时间片给我们的时候,我们要求的时间点可能已经过时了。系统默认的时间片分配是两个单元,每单元10~20微妙(服务器可能有更多的单元,每单元时间是我的记忆,不一定对)。
    加上系统要处理各种中断,线程们并不能预测它们什么时候能得到运行时间。如果你一定要准确定时(比如某些实时工控系统),Windows并不是合适的平台。
      

  3.   

    1 用Timer
    2 用2个Environment.TickCount相减
    3 用性能计数器监视时钟,不过写得不完善,这个实例好像很敏感,如果判断是否>0.4会计数一下子跳过,
      不判断出来的值是0.XXXXX的
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;namespace WindowsApplication46
    {
        public partial class Form1 : Form
        {
            PerformanceCounter PC = null;
            float Start = 0;
            delegate bool TimeElapsed();        public Form1()
            {
                InitializeComponent();
                PC = new PerformanceCounter("Thread", "Elapsed Time", "System/1");
                new Thread(new ThreadStart(RunTime)).Start();
            }        void RunTime()
            {
                while (!(bool)this.Invoke(new TimeElapsed(DoTimeElapsed)))
                    ;
            }        bool DoTimeElapsed()
            {
                if (Start == 0)
                    Start = PC.NextValue();
                this.Text = (PC.NextValue() - Start).ToString();
                return false;
            }
        }
    }
      

  4.   

    运行一下吧,感觉很不错的,但我不知道怎么去判断好,写> ,==,Convent...都有影响,或者换几个实例看看,不用 System/X 的