Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
gettickcount (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern uint GetTickCount();

VB.NET Signature:

<DllImport("kernel32.dll")> _
Private Shared Function GetTickCount() As UInteger
End Function

User-Defined Types:

None.

Notes:

Gets the number of milliseconds elapsed since the system started.

Tips & Tricks:

Sample Code:

    // C# sample for a simple timer to pause before some other code.
    // Just call pPause(delay) where delay is the number of milliseconds. e.g. pPause(1000) will pause for 1 second.
    [ DllImport( "kernel32.dll" ) ]
    public static extern long GetTickCount();

    // Function to simply pause before returning to other code
    private void pPause(long delay)
    {
        long entryTick = GetTickCount();

        // Add code here to disable timers that affect other things in your application
        // tmrTimer.Stop();  // For example...

        while ( GetTickCount() < (entryTick + delay) )
        {
            // Optional - Sleep for a bit...
            // Thread.Sleep(10);

            // Keep the application responsive... You may need to be careful with this and other events accidentally firing when they shouldn't.
            //    If you need, add in code to remove/add event handlers where the timer Stop and Start are.
            Application.DoEvents();

            // Add code here to perhaps update a counter to let the user know that they need to wait...
        }

        // Add code here to enable the timers again
        // tmrTimer.Start();  // For example...
    }

Alternative Managed API:

Do you know one? Please contribute it!

System.Environment.TickCount()

Note, the managed API is subtly different than the Win32 API call. The Win32 API call returns an unsigned int while the managed API returned a signed int. Also note that the MSDN documentation for System.Environment.TickCount() is not correct. This managed API rolls over to int.MinValue and not to 0 after 24.9 days.

UPDATE, The MSDN documentation has been updated to correctly state the int.MinValue 24.9 day rollover. The new documentation also provides an example of how to correctly return a positive value from the method. http://msdn2.microsoft.com/en-us/library/system.environment.tickcount.aspx

TIP: If you need to measure time intervals for longer then 24.9 days you better use:

System.Diagnostics.StopWatch sw = new System.Diagnostics.StopWatch();

sw.Start();

...

long ms = sw.ElapsedMilliseconds;

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions