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

需要在计时器每次运行后,修正计时器的间隔

通过DateTime的Tick来处理     不过这个修正貌似有点不准

public class Meter
{
private Timer ReadingTime;
private DateTime NextTickTimeWholeSeconds; public Meter()
{
DateTime now = DateTime.Now;
NextTickTimeWholeSeconds = new DateTime(now.Ticks - (now.Ticks % TimeSpan.TicksPerSecond), now.Kind); ReadingTime = new Timer();
ReadingTime.Elapsed += new ElapsedEventHandler(PerformReading);
ReadingTime.Interval = GetTimeToNextSecond();
} public void StartMeter()
{
ReadingTime.Start();
} public void StopMeter()
{
ReadingTime.Stop();
} private double GetTimeToNextSecond()
{
NextTickTimeWholeSeconds = NextTickTimeWholeSeconds.AddSeconds();
var interval = NextTickTimeWholeSeconds - DateTime.Now;
return interval.Milliseconds < ? GetTimeToNextSecond() : interval.Milliseconds;
} /// <summary>
/// 定时处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PerformReading(object sender, ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
//Console.WriteLine("Performing reading: " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond);
ReadingTime.Interval = GetTimeToNextSecond();
}
}

C# + high resolution timer

I found a solution to this problem in the following blog:http://web.archive.org/web/20110910100053/http://www.indigo79.net/archives/27#comment-255

It tells you how to use the multimedia timer to have a timer with high frequency. It is working just fine for me!!!

上面的链接无效,在评论中找到这个http://svn.the-starport.net/utfeditor/UTFEditor/MultimediaTimer.cs

主要是通过调用winmm.dll来计时

Timer计时不准确的解决方案 每次都重新调整,修正误差的更多相关文章

  1. Timer计时不准确的问题及解决方法

    在项目中,需要每隔20ms发送一个RTP数据包.一开始使用的是System.Windows.Forms下的Timer类,但是发现明显延迟了.用StopWatch测了一下,发现它的触发间隔居然不是20m ...

  2. jQuery ZeroClipboard中Flash定位不准确的解决方案

    转自波斯马,原文地址<jQuery ZeroClipboard中Flash定位不准确的解决方案> jQuery ZeroClipboard支持在多种浏览器中复制内容到剪贴板,IE.Fire ...

  3. 使用git提交代码到github,每次都要输入用户名和密码的解决方法

    自从使用git提交代码到github后,发现自己使用git的功力增长了不少,但也遇到不少问题.比如,使用git提交代码到github的时候,经常要求输入用户名和密码,类似这种: 网上有这么一种解决方法 ...

  4. macOS 在终端中使用 adb命令,每次都要source ~/.bash_profile 才生效

    macOS下已经配置好Android开发环境,环境变量也添加了,但是在终端中使用adb命令每次都需要source .bash_profile之后才能识别, 否则就提示  zsh: command no ...

  5. 数据库设计时,每个表要不要都设置自增主键ID!(转)

    逻辑数据库设计 - 需要ID(谈主键Id) 本文的目标就是要确认那些使用了主键,却混淆了主键的本质而造成的一种反模式. 一.确立主键规范 每个了解数据库设计的人都知道,主键对于一张表来说是一个很重要, ...

  6. Git push 时每次都需要密码的疑惑

    2015.1.13更新: 在本地搭建Git服务器时,也是有每次操作需要密码的情况. 是因为每次做推送动作时,Git需要认证你是好人.所以需要密码. 可以在 /home/username/.ssh/au ...

  7. 为什么每个请求都要有用户名密码呢,那不是每次都要查询一下了,token,表示这个用户已经验证通过了,在token有效期内,只需要判断token是否有效就可以了

    为什么每个请求都要有用户名密码呢,那不是每次都要查询一下了,token,表示这个用户已经验证通过了,在token有效期内,只需要判断token是否有效就可以了

  8. LISTVIEW嵌套GRIDVIEW的一些处理(点击GRIDVIEW的条目,能够显示他在LISTVIEW中的位置)(对这篇文章的优化处理,不每次都new onItemClickListener)

    前几天写了点击GRIDVIEW的条目,能够显示他在LISTVIEW中的位置,当时的处理是在ListView的适配器里的GetView方法里每次都new GridView的onItemClickList ...

  9. git 设置不需要输入密码, 去除 fetch / pull 代码每次都需要输入密码的烦恼

    https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...

随机推荐

  1. .NET垃圾回收机制 转

    在.NET Framework中,内存中的资源(即所有二进制信息的集合)分为"托管资源"和"非托管资源".托管资源必须接受.NET Framework的CLR( ...

  2. 学习笔记_过滤器应用(粗粒度权限控制(拦截是否登录、拦截用户名admin权限))

    RBAC ->基于角色的权限控制 l  tb_user l  tb_role l  tb_userrole l  tb_menu(增.删.改.查) l  tb_rolemenu 1 说明 我们给 ...

  3. Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法

    Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...

  4. oc for in 的时候nsscanner: nil string argument

    今天偶然发现,oc for in 动态的给一数组加东西,然后嵌套for in 会报nsscanner: nil string argument. 换成for循环就好了,暂时还没找到原因

  5. mongodb write 【摘自网上,只为记录,学习】

    mongodb有一个write concern的设置,作用是保障write operation的可靠性.一般是在client driver里设置的,和db.getLastError()方法关系很大 一 ...

  6. javascript BOM对象 第15节

    <html> <head> <title>浏览器对象</title> <script type="text/javascript&quo ...

  7. (hdu)1950 Bridging signals(最长上升子序列)

    Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...

  8. identifier not found error on function call

    在C++工程中,自定义一个方法 void fgetsDemo(),在main 方法中调用,源代码如下: #include "stdafx.h" #include <stdio ...

  9. C++类继承内存布局(二)

    转自:http://blog.csdn.net/jiangyi711/article/details/4890889# (二 )成员变量 前面介绍完了类布局,接下来考虑不同的继承方式下,访问成员变量的 ...

  10. 网络安全设备Bypass功能介绍及分析

    from:http://netsecurity.51cto.com/art/200910/159948.htm 网络安全平台厂商往往需要用到一项比较特殊的技术,那就是Bypass,那么到底什么是Byp ...