Solved

Get  Millisecond difference

Posted on 2006-06-16
10
321 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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