.NET Compact Framework 上的 DateTime.Now 中的毫秒始终为零?
我想要 Windows Mobile 项目 上的日志有一个时间戳。精度必须至少在一百毫秒范围内。
但是,我对 DateTime.Now
的调用返回一个 DateTime
对象,其 Millisecond
属性设置为零。 Ticks
属性也相应地进行舍入。
如何获得更好的时间精度?
请记住,我的代码在 Compact Framework 3.5 版上运行。我使用 HTC touch Pro 2 设备。
根据 MusiGenesis 的回答,我创建了以下类来解决此问题:
/// <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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Environment.TickCount
将返回 Windows(或 Windows Mobile)自上次重新启动以来运行的毫秒数。要使用此功能,请将这两个表单级变量添加到您的代码中:
在表单的 Load 事件中,执行以下操作:
每当您需要包含毫秒的 DateTime 对象时,执行以下操作:
Environment.TickCount
是一个int
并且该值将在 25 天左右后“环绕”为Int32.MinValue
。如果您的设备要在不重新启动的情况下运行那么长时间,您需要添加一个检查Environment.TickCount
值是否小于上次读取的值,并重置_start
和_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:
In your form's Load event, do this:
Whenever you need a DateTime object with milliseconds, do this:
Environment.TickCount
is anint
and this value will "wrap around" toInt32.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 anEnvironment.TickCount
value that is less than the last value read, and reset both_start
and_startTick
if so.高分辨率计时器怎么样?
What about the High Resolution Timer?
主要替代方案是 System.Diagnostics.Stopwatch 类。
它在 CE 中可用,但请注意
IsHighResolution
属性。在您的设备上它可能是 False,但请检查一下。它与没有 P/Invoke 时一样准确。
The main alternative is the System.Diagnostics.Stopwatch class.
It is available in CE but note the
IsHighResolution
property. It probably is False on your device but do check.It is as accurate as you're going to get without P/Invoke.
在常规框架 v2.0 中,您可以使用 DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") 来获取毫秒。 f 越大意味着精度越高。
In regular framework v2.0 you may use DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") to get miliseconds. More f means more precision.