getting milliseconds

hi,

i'm trying to get milliseconds. but getting always zero value for milliseconds.

Label2.Text = String.Format("{0:d1}.{1:d3}", ck2.Seconds, ck2.Milliseconds)

where ck2 is timespan. I 've seen that CF supports this property. then how

I'm using VS2003.

Thank You.



Answer this question

getting milliseconds

  • Senkwe Chanda

    It all depends on how you are populating your TimeSpan structure. Keep in mind, on Windows CE.Net, DateTime.Now is limited to 1 second granularity:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemdatetimeclassnowtopic.asp

    ".NET Compact Framework Platform Note: In Windows CE .NET, time is specific only to the second. You can get a more precise time span measurement, for example, in milliseconds, by using the TickCount property."

    The following code demonstrates two different methods to calculate your time span. You will notice in the first case, the milliseconds property of the timespan is always zero because of the above limitation on Windows CE based devices.

    DateTime dt1 = DateTime.Now;
    System.Threading.
    Thread
    .Sleep(1111);
    DateTime dt2 = DateTime
    .Now;
    TimeSpan
    ck2 = dt2.Subtract(dt1);
    label1.Text =
    string.Format("{0:d1}.{1:d3}"
    , ck2.Seconds, ck2.Milliseconds);

    int tc1 = Environment.TickCount;
    System.Threading.
    Thread
    .Sleep(1111);
    int tc2 = Environment
    .TickCount;
    ck2 =
    new TimeSpan
    (0, 0, 0, 0, tc2 - tc1);
    label2.Text =
    string.Format("{0:d1}.{1:d3}", ck2.Seconds, ck2.Milliseconds);



  • getting milliseconds