Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() Always Get Same Data in short period

Example Code:
.net core 3

await Task.Delay( 1000 );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

await Task.Delay( 1000 );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

await Task.Delay( 1000 );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Console.WriteLine( "Hello World!!!!!!!!!" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() );

Expect to get different number , but all the same if without any delay.

I wonder how could this happened?
testPicture

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Windows is not a real-time operating system. It has a system clock resolution of 15.6ms.

The default timer resolution on Windows is 15.6 ms – a timer interrupt 64 times a second.

https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/#:~:text=The%20default%20timer%20resolution%20on,consumption%20and%20harm%20battery%20life.

Your code is running faster than the system updates.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading