Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get  Millisecond difference

Posted on 2006-06-16
10
Medium Priority
?
325 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

604 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