Environment.TickCount is not enough

后端 未结 6 1039
别那么骄傲
别那么骄傲 2021-01-13 11:50

I want to know on when was the last time the system was started.

Environment.TickCount will work but it is breaking after 48-49 days because of the limitation of int

6条回答
  •  迷失自我
    2021-01-13 12:36

    I think it's just the way they have implemented it.

    It goes from 0 to max and then goes from min to 0.

    https://msdn.microsoft.com/en-us/library/system.environment.tickcount(v=vs.110).aspx

    I have edited the code you are using from http://www.codeproject.com/Articles/13384/Getting-the-user-idle-time-with-C

    Why don't you just get the Absolute number?

        Public Shared Function GetIdle() As UInteger
            Dim lii As New LASTINPUTINFO()
            lii.cbSize = Convert.ToUInt32((Marshal.SizeOf(lii)))
            GetLastInputInfo(lii)
    
            Dim totalTicks As Long = 0
    
            If Environment.TickCount > 0 Then
                totalTicks = Convert.ToUInt64(Environment.TickCount)
            Else
                totalTicks = Convert.ToUInt64(Environment.TickCount * -1)
            End If
    
            Return Math.Abs(totalTicks - lii.dwTime)
    
        End Function
    

提交回复
热议问题