|
Imports System.ComponentModel
Imports System.Timers
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class SystemIdleTimer
Inherits Component
Private Const INTERNAL_TIMER_INTERVAL As Double = 500
<Description("Event that if fired when idle state is entered.")> _
Public Event OnEnterIdleState(ByVal sender As Object, ByVal e As IdleEventArgs)
<Description("Event that is fired when leaving idle state.")> _
Public Event OnExitIdleState(ByVal sender As Object, ByVal e As IdleEventArgs)
Private ticker As Timers.Timer
Private m_MaxIdleTime As Integer
Private m_LockObject As Object
Private m_IsIdle As Boolean = False
<Description("Maximum idle time in miliseconds.")> _
Public Property MaxIdleTime() As Integer
Get
Return m_MaxIdleTime
End Get
Set(ByVal value As Integer)
m_MaxIdleTime = value
End Set
End Property
Public Sub New()
m_LockObject = New Object()
ticker = New Timers.Timer(INTERNAL_TIMER_INTERVAL)
AddHandler ticker.Elapsed, AddressOf InternalTickerElapsed
End Sub
Public Sub Start()
ticker.Start()
End Sub
Public Sub [Stop]()
ticker.Stop()
SyncLock m_LockObject
m_IsIdle = False
End SyncLock
End Sub
Public ReadOnly Property IsRunning() As Boolean
Get
Return ticker.Enabled
End Get
End Property
Private Sub InternalTickerElapsed(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
Dim idleTime As UInteger = Win32Wrapper.GetIdle()
If idleTime > MaxIdleTime Then
If m_IsIdle = False Then
SyncLock m_LockObject
m_IsIdle = True
End SyncLock
Dim args As New IdleEventArgs(e.SignalTime)
RaiseEvent OnEnterIdleState(Me, args)
End If
Else
If m_IsIdle Then
SyncLock m_LockObject
m_IsIdle = False
End SyncLock
Dim args As New IdleEventArgs(e.SignalTime)
RaiseEvent OnExitIdleState(Me, args)
End If
End If
End Sub
End Class
Public Class IdleEventArgs
Inherits EventArgs
Private m_EventTime As DateTime
Public ReadOnly Property EventTime() As DateTime
Get
Return m_EventTime
End Get
End Property
Public Sub New(ByVal timeOfEvent As DateTime)
m_EventTime = timeOfEvent
End Sub
End Class
Public Class Win32Wrapper
Public Structure LASTINPUTINFO
Public cbSize As UInteger
Public dwTime As UInteger
End Structure
<DllImport("User32.dll")> _
Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Public Shared Function GetIdle() As UInteger
Dim lii As New LASTINPUTINFO()
lii.cbSize = CUInt(Marshal.SizeOf(lii))
GetLastInputInfo(lii)
Return (CUInt(Environment.TickCount) - lii.dwTime)
End Function
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.