10

What is the WinRT replacement for System.Environment.TickCount?

Nick Banks
  • 4,298
  • 5
  • 39
  • 65
  • 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
  • 1
    DateTime.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 Answers2

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
1

It appears System.Environment.TickCount is supported in Windows 8 Windows store apps now.

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