现在需要一个精度比较高的计时器,用C#自带的3个TIMER好像精度不够啊所以需要一个精度比较高的计时器~~~由于我是新学的C#,很多东西不懂也不会写,网上的也不是很看的懂哪位高手能不能帮我写一个高精度的计时器啊~~最好就是代码我贴过来就能用的。。谢谢了!~

解决方案 »

  1.   

    system.timer 好像是这样,总之是system开头的里面有个精度比TIMER控件高的
      

  2.   

    http://www.cnblogs.com/dlbrant/archive/2009/02/17/1392574.html
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Threading;namespace TestPerformance
    {
        internal sealed class PerfTimer
        {
            [DllImport("Kernel32.dll")]
            private static extern bool QueryPerformanceCounter(
                out long lpPerformanceCount);        [DllImport("Kernel32.dll")]
            private static extern bool QueryPerformanceFrequency(
                out long lpFrequency);        private long startTime, stopTime;
            private long freq;        // Constructor
            public PerfTimer()
            {
                startTime = 0;
                stopTime = 0;            if (QueryPerformanceFrequency(out freq) == false)
                {
                    // high-performance counter not supported
                    throw new Win32Exception();
                }
            }        // Start the timer
            public void Start()
            {
                // lets do the waiting threads there work
                Thread.Sleep(0);
                QueryPerformanceCounter(out startTime);
            }        // Stop the timer
            public void Stop()
            {
                QueryPerformanceCounter(out stopTime);
            }        // Returns the duration of the timer (in seconds)
            public double Duration
            {
                get
                {
                    return (double)(stopTime - startTime) / (double)freq;
                }
            }
        }
         class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Press any key to continue");
                Console.ReadKey();            PerfTimer pt = new PerfTimer();     // create a new PerfTimer object
                pt.Start();                             // start the timer            for(int i=0; i<10000; i++)
                {
                    string s = string.Format("This is test round {0}", i);//"This is test round "+i.ToString();
                    Console.WriteLine(s);
                }            pt.Stop();
                Console.WriteLine("Duration: {0} second(s)"n", pt.Duration); // print the duration of the timed code            Console.WriteLine("Press any key to continue");
                Console.ReadKey();
            }
        }}