现在在学C#的winform编程,但是发现timer的时间精度不是横高,有五十多ms,需要提高精度到1ms,要怎么做??网上找了一下,说用API,具体指??还有说用winform编达不到这么高的精度,是不是真的??

解决方案 »

  1.   

    你说的是 UI 的 那个timber 吧, 用 thread 里面的那个 timer
      

  2.   

    WinForm的计时器,并不是线程计时器,也不支持重入,而是通过发送WM_TIMER事件完成。
    所以精准度不可靠,上一次没有执行完,则下一次就不会引发。高性能计时器使用System.Timer或线程计时器
      

  3.   

    线程,或是线程的计时器
    System.Threading.Thread

    System.Threading.Timer
      

  4.   

    参考一下:
    http://topic.csdn.net/u/20120514/16/0235fd54-b240-4035-90a5-04960c52524a.html
      

  5.   

    要完全准确的精度是很难的吧,毕竟你执行你timer中的处理以及自身循环都得耗掉时间啊。。
      

  6.   

    看过了,但是要像tiimer 那样经过一段时间之后诱发事件怎么弄,这个只是返回时间经过。。
      

  7.   

    windows系统就不是实时系统,不能精确计时刚试了下
    System.Threading.Timer, System.Timers.Timer
    精度也只能达到20ms120.0069
    107.0061
    113.0065
    106.0061
    109.0062
    111.0063
    106.0061
    112.0064
    108.0062
    113.0065
    107.0061
    106.006
    118.0068
    101.0058
    109.0062
    115.0066
    106.006
    106.0061
    114.0065
    107.0061
    106.0061
    113.0065
    106.006
    114.0066
    107.0061
    106.006
    115.0066
    106.0061
    107.0061
    114.0065
    106.0061
    112.0064
    109.0062
    107.0061
    108.0062
      

  8.   

    如果你的要求是精确到1ms之内,无论是控件Timer还是Thread Timer都无法达到你的要求,正确的方法是调用APIusing System;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Threading;namespace Win32
    {
      internal class HiPerfTimer
      {
        [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 HiPerfTimer()
        {
          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;
          }
        }
      }
    }
      

  9.   

    要想像timer控件那样每隔一段时间进行一段程序要怎么弄,这个调用只能计算过程的时间吧。
      

  10.   

    http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch.aspx
      

  11.   

    这个也是测试时间间隔的,不是像timer一样能出发的啊
      

  12.   

            [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
            private static extern Int32 设置系统时钟精度(Int32 _毫秒精度_);
            [DllImport("winmm.dll", EntryPoint = "timeEndPeriod")]
            private static extern Int32 恢复系统时钟精度(Int32 _毫秒精度_);            设置系统时钟精度(1);不用后可以             恢复系统时钟精度(1);