在项目中,需要每隔20ms发送一个RTP数据包。一开始使用的是System.Windows.Forms下的Timer类,但是发现明显延迟了。用StopWatch测了一下,发现它的触发间隔居然不是20ms,而是在31ms左右摇摆。换了System.Threading下的Timer和System.Timers下和Timer也不行,一样的问题。

为什么会这样呢?在网上发现了一段非常具有启发性的话,它解释了原因并给出了解决的办法:

    目前,Windows软件一般使用Timer定时器进行定时。Timer定时器是由应用程序响应定时消息WM_TIMER实现定时。Timer定时器是IBM PC硬件和ROM BIOS构造的定时器的简单扩充。PC的ROM初始化8253定时器来产生硬件中断08H,而08H中断的频率为18.2Hz,即至少每隔54.925 ms中断一次。此外,这个定时消息的优先权太低,只有在除WM_PAINT外的所有消息被处理完后,才能得到处理。

    多媒体定时器也是利用系统定时器工作的,但它的工作机理和普通定时器有所不同。首先,多媒体定时器可以按精度要求设置8253的T/C0通道的计数初值,使定时器不存在54.945ms的限制;其次,多媒体定时器不依赖于消息机制,而是用函数产生一个独立的线程,在一定的中断次数到达后,直接调用预先设置好的回调函数进行处理,不必等到应用程序的消息队列为空,从而切实保障了定时中断得到实时响应,使其定时精度可达1ms。

这段话中的多媒体定时器被放在了winmm.dll中。要使用这个定时器,可以通过调用timeSetEvent和timeKillEvent函数来实现。为了便于使用,参照网上的一些资料,对这两个方法进行了封装。现跟大家分享一下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel; public sealed class MillisecondTimer : IComponent, IDisposable
{ //***************************************************** 字 段 *******************************************************************
private static TimerCaps caps;
private int interval;
private bool isRunning;
private int resolution;
private TimerCallback timerCallback;
private int timerID; //***************************************************** 属 性 *******************************************************************
/// <summary>
///
/// </summary>
public int Interval
{
get
{
return this.interval;
}
set
{
if( ( value < caps.periodMin ) || ( value > caps.periodMax ) )
{
throw new Exception( "超出计时范围!" );
}
this.interval = value;
}
} /// <summary>
///
/// </summary>
public bool IsRunning
{
get
{
return this.isRunning;
}
} /// <summary>
///
/// </summary>
public ISite Site
{
set;
get;
} //***************************************************** 事 件 ******************************************************************* public event EventHandler Disposed; // 这个事件实现了IComponet接口
public event EventHandler Tick; //*************************************************** 构造函数和释构函数 ****************************************************************** static MillisecondTimer()
{
timeGetDevCaps( ref caps, Marshal.SizeOf( caps ) );
} public MillisecondTimer()
{
this.interval = caps.periodMin; //
this.resolution = caps.periodMin; // this.isRunning = false;
this.timerCallback = new TimerCallback( this.TimerEventCallback ); } public MillisecondTimer( IContainer container )
: this()
{
container.Add( this );
} ~MillisecondTimer()
{
timeKillEvent( this.timerID );
} //***************************************************** 方 法 ******************************************************************* /// <summary>
///
/// </summary>
public void Start()
{
if( !this.isRunning )
{
this.timerID = timeSetEvent( this.interval, this.resolution, this.timerCallback, , ); // 间隔性地运行 if( this.timerID == )
{
throw new Exception( "无法启动计时器" );
}
this.isRunning = true;
}
} /// <summary>
///
/// </summary>
public void Stop()
{
if( this.isRunning )
{
timeKillEvent( this.timerID );
this.isRunning = false;
}
} /// <summary>
/// 实现IDisposable接口
/// </summary>
public void Dispose()
{
timeKillEvent( this.timerID );
GC.SuppressFinalize( this );
EventHandler disposed = this.Disposed;
if( disposed != null )
{
disposed( this, EventArgs.Empty );
}
} //*************************************************** 内部函数 ******************************************************************
[DllImport( "winmm.dll" )]
private static extern int timeSetEvent( int delay, int resolution, TimerCallback callback, int user, int mode ); [DllImport( "winmm.dll" )]
private static extern int timeKillEvent( int id ); [DllImport( "winmm.dll" )]
private static extern int timeGetDevCaps( ref TimerCaps caps, int sizeOfTimerCaps );
// The timeGetDevCaps function queries the timer device to determine its resolution. private void TimerEventCallback( int id, int msg, int user, int param1, int param2 )
{
if( this.Tick != null )
{
this.Tick( this, null ); // 引发事件
}
} //*************************************************** 内部类型 ****************************************************************** private delegate void TimerCallback( int id, int msg, int user, int param1, int param2 ); // timeSetEvent所对应的回调函数的签名 /// <summary>
/// 定时器的分辨率(resolution)。单位是ms,毫秒?
/// </summary>
[StructLayout( LayoutKind.Sequential )]
private struct TimerCaps
{
public int periodMin;
public int periodMax;
} }

调用方法:

        MillisecondTimer timer1;

        private void button1_Click( object sender, EventArgs e )
{
timer1 = new MillisecondTimer();
timer1.Interval = ;
timer1.Tick += new EventHandler( timer1_Tick );
timer1.Start();
} int i;
void timer1_Tick( object sender, EventArgs e )
{
Console.WriteLine( i++ );
} private void button2_Click( object sender, EventArgs e )
{
timer1.Stop();
timer1.Dispose();
}

参考:

.NET中的三种Timer的区别和用法(转)

Timer计时不准确的问题及解决方法的更多相关文章

  1. Timer计时不准确的解决方案 每次都重新调整,修正误差

    http://stackoverflow.com/questions/29722838/system-timers-timer-steadily-increasing-the-interval 需要在 ...

  2. php浮点数计算比较及取整不准确解决方法

    原文:php浮点数计算比较及取整不准确解决方法 php有意思的现象,应该是很多编程语言都会有这样的现象.这个是因为计算机的本身对浮点数识别的问题..... $f = 0.58; var_dump(in ...

  3. 阿里云 ECS centos java timer进程异常/混乱......的解决方法

    之前就知道timer进程长久运行容易出问题,所以一直对timer进行了很长一段时间的日志监控和数据库记录,大概观察了几个月,没发现过问题....然后就没管理了,数据库记录也没做了,昨天这问题就来了,t ...

  4. ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法

    一.ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法 二.找到ux目录下的TabCloseMenu.js文件,将内容替换成下面代码. 三.代码: /** * Plugin f ...

  5. 小程序 RecorderManager计时不准确问题

    官方文档:RecorderManager 录音管理器,内部实现计时不准确.有以下俩个问题: 点击暂停继续,当录音结束时,stop返回的时间包含了暂停的那一段时间. 正常录音,录音文件的时长有概率少个1 ...

  6. window.onresize 多次触发的解决方法

    用了window.onresize但是发现每次 onresize 后页面中状态总是不对,下面与大家分享下onresize 事件多次触发的解决方法. 之前做一个扩展,需要在改变窗口大小的时候保证页面显示 ...

  7. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  8. Sublime Text 无法使用Package Control的解决方法 以及 常用的插件安装过程

    大概一个月之前给 Macbook air 装 Sublime Text 3 的时候,遇到过这个问题,当时解决了,现在回想,感觉忘的七七八八了,赶紧趁着还没有全忘光的时候记下来,当时的过程记得不一定准确 ...

  9. android手机出现sqlite3 not found的解决方法

    解决方法如下: 1.如果/system目录为不可读写的,需要挂载为读写: C:\Users\easteq>adb shell root@android:/ # mount -o remount, ...

随机推荐

  1. 使用linq语句进行联表查询

    假设你有一个父表(例如:汽车),其关联一个子表,例如轮子(一对多).现在你想对于所有的父表汽车,遍历所有汽车,然后打印出来所有轮子的信息.默认的做法将是: SELECT CarId FROM Cars ...

  2. C#语言————第四章 常用Convert类的类型转换方法

    方法 说明Convert.ToInt32() 转换为整型(int 型)Convert.ToStringle() 转换为单精度浮点型(float 型)Convert.ToDouble()  转换为双精度 ...

  3. Visual Stuido插件大全

    JS Enhancements 使用JS能像C#代码一样折叠成块 Code Compare Code Compare is a powerful file and folder comparison ...

  4. 免费ARP

    1. 免费ARP基本概念 免费ARP,也叫Gratutious ARP.无故ARP.这种ARP不同于一般的ARP请求,它的Sender IP和Target IP字段是相同的,相当于是请求自己的IP地址 ...

  5. 什么是Docker?

      概观 Docker是推动集装箱运动的公司,也是唯一一家能够解决混合云中的每个应用的集装箱平台提供商.当今的企业面临数字化转型的压力,但受到现有应用程序和基础架构的制约,同时合理化日益多样化的云,数 ...

  6. 【问题集】VS新建项目——失败——弹出“未将对象引用设置到对象的实例”

  7. PLSQL无法粘贴复制

    有2个原因会导致这个问题发生: 一:快捷键设置不正确,按照网上的设置方法把复制粘贴的快捷键重新设置一下,然后重启plsql 二:远程桌面连接开着,关闭后试下(亲测有效)

  8. Python基础知识:类

    初级篇 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 1.面向对象三大特性 ...

  9. 【Excel】SUMIF函数的兼容性

    兼容性非常强的两个函数 SUMIF() 说兼容性,当然得说SUMIF了. 来,我们先举个例子. 现有一个表格,算起来只有"科目划分"."发生额"两列内容,但是折 ...

  10. Hadoop2.7.6_08_Federation联邦机制

    前言: 本文章是在  Hadoop2.7.6_07_HA高可用  的基础上完成的,所以不清楚的可参见这篇文章. 1. Hadoop的federation机制 文件的元数据是放在namenode上的,只 ...