Environment.TickCount is not enough

c# 2019. 1. 3. 14:22

Environment.TickCount is not enough


- Environment.TickCount will overflow after approximately 25 days, because the return value is a 32-bit integer.

https://stackoverflow.com/questions/756462/elapsed-time-with-environment-tickcount-avoiding-the-wrap


Use Stopwatch

Stopwatch mStopwatch = new Stopwatch();

mStopwatch.Start();

long elapsedMs = mStopwatch.ElapsedMilliseconds;

https://stackoverflow.com/questions/4645171/environment-tickcount-is-not-enough/4645208


Use GetTickCount64


This function does the same as GetTickCount but returns the result as a 64 bit integer.

Please note that the function result wraps to 0 after about 584 million years.

http://www.on-time.com/rtos-32-docs/rtkernel-32/programming-manual/alternate-apis-for/win32/gettickcount64.htm

https://docs.microsoft.com/ko-kr/windows/desktop/api/sysinfoapi/nf-sysinfoapi-gettickcount64


using System.Runtime.InteropServices;

...

[DllImport("kernel32.dll") ]

public static extern UInt64 GetTickCount64();

...

var tickCount64 = GetTickCount64();