What is the WinRT replacement for System.Environment.TickCount?
Asked
Active
Viewed 2,951 times
10
-
What do you need it for? For many uses `DateTime.UtcNow` is perfectly fine, but it's not equivalent. – CodesInChaos Apr 29 '12 at 19:52
-
1DateTime.UtcNow is not a replacement if you need monotonic time. UtcNow is an arbitrary value. – usr Apr 29 '12 at 20:15
-
TickCount is an arbitrary value as well. DateTime.UtcNow.Tick is monotonic. – Hans Passant Apr 29 '12 at 20:23
-
3@Hans Passant, UtcNow can jump backwars if the user adjust the time back. Env.TickCount is monotonic. This issue actually caused a bug in the timeout implementation of ManualResetEventSlim which I reported and which was fixed. When the user changed the clock time the timeout elapsed suddenly or never elapsed. – usr Apr 29 '12 at 20:24
-
Hmm, this normally requires an explicit call to CultureInfo.ClearCachedData. Glad that the bug was fixed :) – Hans Passant Apr 29 '12 at 20:29
-
System.Environment.TickCount is included in the subset of the .Net framework available to .Net windows store projects. – Craig Gidney Sep 27 '12 at 20:18
2 Answers
7
It should be available, because it isn't a problem. But it is not, a [TypeForwardedTo] hangup I guess because GetTickCount() isn't on the white list and .NET never adopted GetTickCount64. The standard fallback works fine, you can use pinvoke the call the native Windows function. I verified that a program that uses it passes the Windows App Certification Kit test.
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern long GetTickCount64();
Note that it returns a long, not an int. You can simply cast to int to truncate if that's important in C# (but not vb.net, just lie about the return type)

Hans Passant
- 922,412
- 146
- 1,693
- 2,536
-
So I am confused. If they are allowed for Metro Apps, then why is it not present in the C# APIs by default? – Nick Banks May 04 '12 at 00:39
-
I tried to explain it in the answer. Don't shoot the messenger, I prefer answers that solve a problem. – Hans Passant May 04 '12 at 00:41
-
Yeah, I'm sorry. I didn't mean to harp on you. I wonder if this will change with the final release. Thanks though. – Nick Banks May 04 '12 at 00:43
-
2
1
It appears System.Environment.TickCount is supported in Windows 8 Windows store apps now.

Anthony Wieser
- 4,351
- 1
- 23
- 25