?
Solved

Get  Millisecond difference

Posted on 2006-06-16
10
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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
 
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 800 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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