您现在的位置: 天下网吧 >> 网吧天地 >> 天下码农 >> 微信小程序 >> 正文

提供一个获取高精度时间类

2010-12-22vczx佚名

如果你觉得用 DotNet 自带的 DateTime 获取的时间精度不够,解决的方法是通过调用 QueryPerformanceFrequency 和 QueryPerformanceCounter这两个API来实现。

/// <summary>
/// 获取时间的精度
/// </summary>
/// <param name="PerformanceFrequency"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
static private extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
/// <summary>
/// 获取时间计数
/// </summary>
/// <param name="PerformanceCount"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
static private extern bool QueryPerformanceCounter(ref long PerformanceCount);

下面是完整的封装代码:

/// <summary>
/// 定义一个高精度的时间类
/// </summary>
    public class Timer
{
#region private members
private long ticksPerSecond = 0;
private long elapsedTime = 0;
private long baseTime = 0;
#endregion
#region windows API
/// <summary>
/// 获取时间的精度
/// </summary>
/// <param name="PerformanceFrequency"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
static private extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
/// <summary>
/// 获取时间计数
/// </summary>
/// <param name="PerformanceCount"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
static private extern bool QueryPerformanceCounter(ref long PerformanceCount);
#endregion
#region constructors
/// <summary>
/// new
/// </summary>
        public Timer()
{
// Use QueryPerformanceFrequency to get frequency of the timer
            if (!QueryPerformanceFrequency(ref ticksPerSecond))
throw new ApplicationException("Timer: Performance Frequency Unavailable");
Reset();
}
#endregion
#region public methods
/// <summary>
/// 重置时间相关计数器
/// </summary>
        public void Reset()
{
long time = 0;
QueryPerformanceCounter(ref time);
baseTime = time;
elapsedTime = 0;
}
/// <summary>
/// 获取当前与最近一次 reset 时间差
/// </summary>
/// <returns>The time since last reset.</returns>
        public double GetTime()
{
long time = 0;
QueryPerformanceCounter(ref time);
return (double)(time - baseTime) / (double)ticksPerSecond;
}
/// <summary>
/// 获取当前系统的时间 ticks 数
/// </summary>
/// <returns>The current time in seconds.</returns>
        public double GetAbsoluteTime()
{
long time = 0;
QueryPerformanceCounter(ref time);
return (double)time / (double)ticksPerSecond;
}
/// <summary>
/// 获取此次与上次调用此方法的两次时间差
/// </summary>
/// <returns>The number of seconds since last call of this function.</returns>
        public double GetElapsedTime()
{
long time = 0;
QueryPerformanceCounter(ref time);
double absoluteTime = (double)(time - elapsedTime) / (double)ticksPerSecond;
elapsedTime = time;
return absoluteTime;
}
#endregion
}
调用代码:
Timer t = new Timer();
t.GetAbsoluteTime();
t.GetElapsedTime();
t.Reset();
t.GetTime();

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛 https://bbs.txwb.com

关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩

本文来源:vczx 作者:佚名

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系邮箱:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧 网吧天下