locked
How to get system uptime?

    Question

  • So I have been using this to be getting my system uptime and it has been working fine ever since my pc went over 25 days uptime. It starts counting down like -25 days then the next day its -24 and so on.

    This what im using.

       Dim nTicks As Double
            Dim nDays As Integer
            Dim nHours As Integer
            Dim nMin As Integer
            Dim nSec As Integer
            nTicks = Environment.TickCount
            nTicks = nTicks / 1000
            nDays = Int(nTicks / (3600 * 24))
            nTicks = nTicks - (Int(nTicks / (3600 * 24)) * (3600 * 24))
            nHours = Int(nTicks / 3600)
            nTicks = nTicks - (Int(nTicks / 3600) * 3600)
            nMin = Int(nTicks / 60)
            nTicks = nTicks - (Int(nTicks / 60) * 60)
            nSec = nTicks
            me.text = "System Uptime: " & Str(nDays) & " Days," & Str(nHours) & " Hours," & Str(nMin) & " Minutes," & Str(nSec) & " Seconds"
    Saturday, December 09, 2006 3:27 AM

Answers

  • You could get an extra 25 days if you would (could) convert the value of Environment.TickCount to UInt32.  WMI offers a permanent solution.  Project + Add Reference, add System.Management.  Write this code:

    Imports System.Management
    Imports System.Globalization
    ...
      Public Shared Function LastBootTime() As Date
        Dim q As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_OperatingSystem")
        For Each result As ManagementObject In q.Get()
          Dim s As String = CStr(result("LastBootUpTime"))
          s = s.Substring(0, s.IndexOf("."c))
          Return DateTime.ParseExact(s, "yyyyMMddhhmmss", CultureInfo.InvariantCulture)
        Next

    Sunday, December 10, 2006 10:32 AM

All replies

  • EnvironmentTickCount returns a 32 bit signed integer which can hold a maximum value of 2147483647.

    When it reaches that value, after 24.9 days it changes to -2147483647 and continues counting back to zero.

    Saturday, December 09, 2006 2:25 PM
  • Well, what do I do then?
    Saturday, December 09, 2006 9:03 PM
  • Well you need to store the number of times the tickcount has gone round and add in an appropriate amount to nTicks before doing the rest of the calculations. 

    e.g.  If you had a variable Count, each time the tickcount turns negative or goes through zero the variable would be incremented by 1.  The tickcount would then be incremented each time by Count * Int32.MaxValue

    If the tickcount is negative then you should subtract it from Int32.MaxValue before using it.

    In pseudocode

    If TickCount changes sign then
        Count +=1
    EndIf

    If TickCount < 0 then
        TickCount = TickCount + Int32.MaxValue
    EndIf

    TickCount = TickCount + Count * Int32.MaxValue

    Then all you have to do is wait another 25 days to see if it works !!

    Saturday, December 09, 2006 11:09 PM
  • You could get an extra 25 days if you would (could) convert the value of Environment.TickCount to UInt32.  WMI offers a permanent solution.  Project + Add Reference, add System.Management.  Write this code:

    Imports System.Management
    Imports System.Globalization
    ...
      Public Shared Function LastBootTime() As Date
        Dim q As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_OperatingSystem")
        For Each result As ManagementObject In q.Get()
          Dim s As String = CStr(result("LastBootUpTime"))
          s = s.Substring(0, s.IndexOf("."c))
          Return DateTime.ParseExact(s, "yyyyMMddhhmmss", CultureInfo.InvariantCulture)
        Next

    Sunday, December 10, 2006 10:32 AM
  • Hi Joshizzle,

    Were you able to get the system uptime to show more then 25 days??
    If so do you have sample code?
    Monday, January 05, 2009 9:54 PM