Support Forums
[Snippet] Get OS Uptime - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [Snippet] Get OS Uptime (/showthread.php?tid=18724)



[Snippet] Get OS Uptime - euverve - 05-12-2011

[Image: mM540.png]

Full Codes:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        Timer1.Enabled = True
        Timer1.Interval = 10
        Timer1.Start()
    End Sub

    Sub Timer1_Tick() Handles Timer1.Tick
        TextBox1.Text = GetOSUptime()
    End Sub

    Private Shared Function GetOSUptime() As String
        'Gets the number of milliseconds elapsed since the system started in minutes.

        Dim intDays As Integer
        Dim intHours As Integer
        Dim intMinutes As Integer
        Dim intSeconds As Integer
        Dim intRemainder As Integer
        Dim intTicks As Integer

        ' initialize string variables
        Dim strDays As String = ""
        Dim strHours As String = ""
        Dim strMinutes As String = ""
        Dim strSeconds As String = ""

        Try
            ' updates tick counter intTicks
            intTicks = System.Environment.TickCount

            ' there are  86400000 milliseconds in one day,
            ' 'compute whole days and get remainder
            Do
                intDays = Int(intTicks \ 86400000)
                intRemainder = intTicks Mod 86400000
            Loop Until intRemainder <= 86400000

            ' there are 3600000 milliseconds in one hour,
            ' 'compute whole hours and get remainder
            Do
                intHours = Int(intRemainder \ 3600000)
                intRemainder = intRemainder Mod 3600000
            Loop Until intRemainder <= 3600000

            ' there are 60000 milliseconds in one minute,
            ' compute whole minutes and get remainder
            Do
                intMinutes = Int(intRemainder \ 60000)
                intRemainder = intRemainder Mod 60000
            Loop Until intRemainder <= 60000

            ' there are 1000 milliseconds in one second,
            ' compute whole seconds and get remainder
            Do
                intSeconds = Int(intRemainder \ 1000)
                intRemainder = intRemainder Mod 1000
            Loop Until intRemainder <= 1000

            ' format days
            If intDays = 0 Then
                strDays = ""
            ElseIf Trim(CStr(intDays)).Length = 1 Then
                strDays = String.Format(" {0}:", Trim(CStr(intDays)))
            ElseIf Trim(CStr(intDays)).Length = 2 Then
                strDays = Trim(CStr(intDays)) & ":"
            End If

            ' format hours
            If intHours = 0 And intDays = 0 Then
                strHours = ""
            ElseIf Trim(CStr(intHours)).Length = 1 Then
                strHours = String.Format("0{0}:", Trim(CStr(intHours)))
            ElseIf Trim(CStr(intHours)).Length = 2 Then
                strHours = Trim(CStr(intHours)) & ":"
            End If

            ' format minutes
            If intMinutes = 0 Then
                strMinutes = "00" & ":"
            ElseIf Trim(CStr(intMinutes)).Length = 1 Then
                strMinutes = String.Format("0{0}:", Trim(CStr(intMinutes)))
            ElseIf Trim(CStr(intMinutes)).Length = 2 Then
                strMinutes = Trim(CStr(intMinutes)) & ":"
            End If

            ' format seconds
            If intSeconds = 0 Then
                strSeconds = "00"
            ElseIf Trim(CStr(intSeconds)).Length = 1 Then
                strSeconds = "0" & Trim(CStr(intSeconds))
            ElseIf Trim(CStr(intSeconds)).Length = 2 Then
                strSeconds = Trim(CStr(intSeconds))
            End If

            ' return time string
            Return strDays & strHours & strMinutes & strSeconds
        Catch ex As Exception
            Return ""
        End Try
    End Function

End Class