Я хочу иметь метку времени для журналов в проекте 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);
}
}
}