1

I have been told to avoid using Environment.TickCount() as it overflows in about a month of uptime and can mess up my code, so I was wondering if DateTime.Now.Ticks() would be a safer choice for long terms ? I mean when will it overflow ?

Heidi
  • 141
  • 9
  • 1
    Environment.TickCount is an int, and only 32 bits long, hence it will easily overflow. DateTime.Now.Ticks is a long and 64 bits long. The max value is 9,223,372,036,854,775,807 and is quite a few ticks indeed. – J. Steen Nov 19 '14 at 12:50
  • 2
    `Environment.TickCount` will overflow in ~49.7 days. `DateTime.Now.Ticks` should be good for almost 100 billion days. – leppie Nov 19 '14 at 12:50
  • http://stackoverflow.com/questions/243351/environment-tickcount-vs-datetime-now – Tim Schmelter Nov 19 '14 at 12:51
  • To answer your question: yes it can overflow. in exactly 9,223,372,036,854,775,807 seconds. I think the problem of the sun going supernova before that time is something you should worry about more than a timer that is overflowing ;-) – DoXicK Nov 19 '14 at 12:51
  • It depends what you need to do. [Environment.TickCount() is only for 48.7 days](http://msdn.microsoft.com/en-us/library/system.environment.tickcount(v=vs.110).aspx) – Renatas M. Nov 19 '14 at 12:52
  • We're just a massive bunch of smartasses, aren't we. =) – J. Steen Nov 19 '14 at 12:52
  • Also [this](http://www.informit.com/blogs/blog.aspx?uk=Highly-unlikely-does-not-mean-impossible) – Sriram Sakthivel Nov 19 '14 at 12:55
  • Thank you guys! But I don't see why this question was flagged as a duplicate. The link provided didn't answer my question, I've already read it before posting – Heidi Nov 19 '14 at 12:55
  • That's because you didn't tell where you using ticks, I guess everyone forecasted from magic ball that you measuring elapsed time – Renatas M. Nov 19 '14 at 12:59
  • @Heidi: what part of your question does the proposed duplicate _not_ answer? Have you read all answers? – Tim Schmelter Nov 19 '14 at 13:04

0 Answers0