Solved

Get  Millisecond difference

Posted on 2006-06-16
10
319 Views
Last Modified: 2009-07-29
Hello,

Iam trying to get the millisecond period between 2 times but seams that this is a problem in very small periods.

Try this:
===============================

TextBox1.Text = Environment.TickCount
Threading.Thread.Sleep(1)
TextBox2.Text = Environment.TickCount
=============================== You will get the same value in both text boxes

I tryed a lot of other ways like using the datetime but no way, it give the same value, and very very rare to give a different value.



Can anybody support me the right way to do that?



Thanks
0
Comment
Question by:amrelgarhy81
10 Comments
 
LVL 2

Expert Comment

by:rraghvendra
ID: 16918912
use timespan

      DateTime dt1 = DateTime.Now;
                  DateTime dt2 = DateTime.Now;
                  TimeSpan ts = new TimeSpan();
                  ts= dt2-dt1;
                  Console.WriteLine(ts.TotalMilliseconds);
             
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16919044
Hello,

Thanks for reply but:

I tried this:
        Dim d1 As DateTime = DateTime.Now
        Threading.Thread.Sleep(1)
        Dim t As New TimeSpan(1)
        Dim d2 As DateTime = DateTime.Now
        t = d2 - d1
        Me.TextBox2.Text = t.TotalMilliseconds

But most the  times TextBox2.Text value = 0 and very rare = 15.625

Can you let me know why?
And there is  another way?



Thanks for effort.
0
 
LVL 4

Assisted Solution

by:fffej78
fffej78 earned 50 total points
ID: 16919305
The resolution of those timers suck.  They aren't good enough for millisecond accuracy.  Try to use native Windows timings functions, which are an order of magnitude more accurate and give greater resolution.

I'm no VB expert, but try these out for size.

<SuppressUnmanagedCodeSecurity()> _
Private Declare Auto Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef lpPerformanceCount As Long) As Boolean

<SuppressUnmanagedCodeSecurity()> _
Private Declare Auto Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef lpFrequency As Long) As Boolean

Dim freq as long
dim start as long
dim stop as long

QueryPerformanceFrequency( freq )
QueryPerformanceCounter(start)

' do your thing here

QueryPerformanceCounter(stop)

(stop - start ) / freq should give you the time in seconds, much more accurately.

Hope that helps.

Jeff
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16919319
No problem write it in C#
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16919328
Well, I think the code above should work, basically you need the API import for QueryPerformanceCounter and QueryPerformanceFreq (the frequency of how often you tick).  That should sort out the timing difficulties.  Are you getting errors using the code given above?

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16919335
no,no errors
I will try this APIs and tell you the result because iam busy now a little.

Thanks,
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 16919359
See System.Diagnostics.Stopwatch class, intermally it uses high precision timer.
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16919377
I didn't know about the StopWatch class, definitely a more attractive option if you are using .NET framework 2
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16919380
ok AlexFM thanks i will try today later and i have feeling that i will work fine.
0
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16919460
Perfect AlexFM
Thanks you very much, it gave me the result i was looking for.
The code i tested with is:
        Dim sw As New System.Diagnostics.Stopwatch()
        sw.Start()
        Threading.Thread.Sleep(2)
        Me.TextBox2.Text = sw.ElapsedMilliseconds

And if you please Sir AlexFM i will give fffej78 some points because he worked hard with me.


Thanks guys.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now