Created
December 28, 2017 10:41
-
-
Save Jalalx/b69b0d1f672869e874ced56bb3beba69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Read more at: https://stackoverflow.com/a/14369695/375958 | |
/// </summary> | |
public static class HighResolutionDateTime | |
{ | |
private static long lastTimeStamp = DateTime.UtcNow.Ticks; | |
public static long UtcNowTicks | |
{ | |
get | |
{ | |
long original, newValue; | |
do | |
{ | |
original = lastTimeStamp; | |
long now = DateTime.UtcNow.Ticks; | |
newValue = Math.Max(now, original + 1L); | |
} while (Interlocked.CompareExchange(ref lastTimeStamp, newValue, original) != original); | |
return newValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment