LastInput.time is an Integer and m_idleTime is an Integer too. This line sometimes generates an Overflow exception, I think that this happens when both values are big negative values.
(Environment.TickCount - lastInput.time) > m_idleTime
How can I avoid that? With casting?
(CType(Environment.TickCount,Long) - CType(lastInput.time,Long)) > m_idleTime
Or maybe with this cast?
CType((Environment.TickCount - lastInput.time),Long) > m_idleTime
Thanks in advance.
EDIT: I’m using the GetLastInputInfo method to check how many time has been the computer idle. I have declared the return value from the call this way:
<StructLayout(LayoutKind.Sequential)> Public Structure StructLastInputInfo
<MarshalAs(UnmanagedType.U4)> Dim size As Integer
<MarshalAs(UnmanagedType.U4)> Dim time As Integer
End Structure
So I think that when the Environment.TickCount returns a negative value the same will happen with the GetLastInputInfo, right? But then the values of the substraction will be wrong because they will be negative, so as far as I see the problem what should be done is this:
Math.Abs(CType(Environment.TickCount, Long) - CType(lastInput.time, Long)) > m_idleTime
What do you think?
That’s how I fixed the problem:
This code works because you get the bytes of the Integer and just use them as if they where an UInteger, this does not throw OverflowException.
The only problem I’ve seen is that you have a 1 unit error when doing the subtraction when the sign of one item has changed, not sure about what is happening but anyway having 1 tick error is not an issue.