Solved

Get  Millisecond difference

Posted on 2006-06-16
10
322 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 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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