【发布时间】:2011-01-27 08:45:45
【问题描述】:
LastInput.time 是一个整数,m_idleTime 也是一个整数。此行有时会产生溢出异常,我认为当两个值都是大负值时会发生这种情况。
(Environment.TickCount - lastInput.time) > m_idleTime
我怎样才能避免这种情况?有铸造吗?
(CType(Environment.TickCount,Long) - CType(lastInput.time,Long)) > m_idleTime
或者也许是这个演员阵容?
CType((Environment.TickCount - lastInput.time),Long) > m_idleTime
提前致谢。
编辑:我正在使用 GetLastInputInfo 方法来检查计算机空闲了多少时间。我已经通过这种方式声明了调用的返回值:
<StructLayout(LayoutKind.Sequential)> Public Structure StructLastInputInfo
<MarshalAs(UnmanagedType.U4)> Dim size As Integer
<MarshalAs(UnmanagedType.U4)> Dim time As Integer
End Structure
所以我认为当 Environment.TickCount 返回负值时,GetLastInputInfo 也会发生同样的情况,对吧?但是减法的值会是错误的,因为它们是负数,所以据我所知,应该做的是:
Math.Abs(CType(Environment.TickCount, Long) - CType(lastInput.time, Long)) > m_idleTime
你怎么看?
【问题讨论】:
-
在我看来,第一个选项将解决您的问题。第二个选项不会对 StackOverflow 执行任何操作,因为它会在将结果转换为 Long 之前发生。
标签: .net vb.net exception overflow casting