毫秒DateTime.Now在.NET Compact Framework中始终为零? [英] Milliseconds in DateTime.Now on .NET Compact Framework always zero?

查看:152
本文介绍了毫秒DateTime.Now在.NET Compact Framework中始终为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个时间戳作为日志上的 Windows Mobile的项目。精度必须在范围内的几百毫秒最少。



不过我呼吁 DateTime.Now 返回的DateTime 与的毫秒属性设置为零的对象。另外,属性会相应四舍五入。



如何获得更好的时间准确性?结果
记住,我的代码运行在上Compact Framework的版本3.5。我用的HTC Touch Pro 2的设备



根据来自MusiGenesis我创建了以下类,解决了这个问题的答案:

  ///<总结> 
///一个更精确的实现在移动设备上的一些日期时间属性。
///< /总结>
///< devdoc方式>在测试了一款HTC Touch Pro2的< / devdoc>
公共静态类DateTimePrecisely
{
///<总结>
///记得,当创建此模式的开始时间。
///< /总结>
私有静态的DateTime _start = DateTime.Now;
///<总结>
///记住创建此模式时,系统正常运行时间刻度。这种
///作为一个更精确的时间提供为DateTime.Now可以做。
///< /总结>
私有静态诠释_startTick = Environment.TickCount;

///<总结>
///获取精确地设置为当前日期和时间,在此计算机上DateTime对象,表示为本地时间。
///< /总结>
///<&回报GT;< /回报>
公共静态的DateTime现在
{
得到
{
返回_start.AddMilliseconds(Environment.TickCount - _startTick);
}
}
}


解决方案

Environment.TickCount 将返回窗口(或Windows Mobile)已自上次重新启动运行的毫秒数。



要使用此功能,添加这两个窗体级变量,你的代码:

 私人的DateTime _start; 
私人诠释_startTick;

在窗体的Load事件,做到这一点:

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
_start = DateTime.Now;
_startTick = Environment.TickCount;
}



当你需要毫秒DateTime对象,做到这一点:

  DateTime时间戳= 
_start.AddMilliseconds(Environment.TickCount - _startTick);



Environment.TickCount INT 和后25天左右,这个值将环绕,以 Int32.MinValue 。如果您的设备要运行那么久而不需要重新启动,你需要增加一个检查为小于读取的最后一个值的 Environment.TickCount 值,复位两个 _start _startTick 如果是这样。


i want to have a time stamp for logs on a Windows Mobile project. The accuracy must be in the range a hundred milliseconds at least.

However my call to DateTime.Now returns a DateTime object with the Millisecond property set to zero. Also the Ticks property is rounded accordingly.

How to get better time accuracy?
Remember, that my code runs on on the Compact Framework, version 3.5. I use a HTC touch Pro 2 device.

Based on the answer from MusiGenesis i have created the following class which solved this problem:

/// <summary>
/// A more precisely implementation of some DateTime properties on mobile devices.
/// </summary>
/// <devdoc>Tested on a HTC Touch Pro2.</devdoc>
public static class DateTimePrecisely
{
    /// <summary>
    /// Remembers the start time when this model was created.
    /// </summary>
    private static DateTime _start = DateTime.Now;
    /// <summary>
    /// Remembers the system uptime ticks when this model was created. This
    /// serves as a more precise time provider as DateTime.Now can do.
    /// </summary>
    private static int _startTick = Environment.TickCount;

    /// <summary>
    /// Gets a DateTime object that is set exactly to the current date and time on this computer, expressed as the local time.
    /// </summary>
    /// <returns></returns>
    public static DateTime Now
    {
        get
        {
            return _start.AddMilliseconds(Environment.TickCount - _startTick);
        }
    }
}

解决方案

Environment.TickCount will return the number of milliseconds that Windows (or Windows Mobile) has been running since the last reboot.

To use this, add these two form-level variables to your code:

private DateTime _start;
private int _startTick;

In your form's Load event, do this:

private void Form1_Load(object sender, EventArgs e)
{
    _start = DateTime.Now;
    _startTick = Environment.TickCount;
}

Whenever you need a DateTime object with milliseconds, do this:

DateTime timeStamp = 
    _start.AddMilliseconds(Environment.TickCount - _startTick);

Environment.TickCount is an int and this value will "wrap around" to Int32.MinValue after 25 days or so. If your device is going to be running that long without restarting, you'll want to add a check for an Environment.TickCount value that is less than the last value read, and reset both _start and _startTick if so.

这篇关于毫秒DateTime.Now在.NET Compact Framework中始终为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆