摘要:LIBClassNew.vb - AutoCloseLogOff
這次的版本 比上一個AutoCloseForm 的好
這是質的轉變
花了不少時間找window api
如果知道電腦是idle or not?

Public Sub SetAutoLogOffMainForm() Sub SetAutoLogOffMainForm(ByVal pform As Form, ByVal piTimeOutInSecond As Integer, ByVal piReminderTimeInSecond As Integer, Optional ByVal pFunc As DFunc0 = Nothing)
Try
ClsAutoLogOff = New ClassAutoLogOff
If Not pFunc Is Nothing Then
ClsAutoLogOff.SetFunc(pFunc)
End If
ClsAutoLogOff.SetMainForm(pform)
ClsAutoLogOff.SetTimerInterval(piTimeOutInSecond)
ClsAutoLogOff.SetReminderTime(piReminderTimeInSecond)
ClsAutoLogOff.StartTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub SetAutoLogOffSubForm() Sub SetAutoLogOffSubForm(ByVal pform As System.Windows.Forms.Form)
Try
If Not ClsAutoLogOff Is Nothing Then
ClsAutoLogOff.SetFrom(pform)
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub FormAutoLogOff() Sub FormAutoLogOff(ByVal pform As Form, ByVal piTimeOutInSecond As Integer, ByVal piReminderTimeInSecond As Integer, Optional ByVal pFunc As DFunc0 = Nothing)
Try
Dim ClsAutoLogOff As New ClassAutoLogOff
If Not pFunc Is Nothing Then
ClsAutoLogOff.SetFunc(pFunc)
End If
ClsAutoLogOff.HasMainFormOnly()
ClsAutoLogOff.SetMainForm(pform)
ClsAutoLogOff.SetTimerInterval(piTimeOutInSecond)
ClsAutoLogOff.SetReminderTime(piReminderTimeInSecond)
ClsAutoLogOff.StartTimer()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

" ClassAutoLogOff "#Region " ClassAutoLogOff "
''' <summary>
''' When there is no action in the form in XX seconds. Then AutoClose.
''' Desc :
''' History :
''' 2009-09-02 - Developing
''' </summary>
'''
Public Class ClassAutoLogOff

" PC Idle Time "#Region " PC Idle Time "

Private Declare Function GetLastInputInfo Lib "user32" () Declare Function GetLastInputInfo Lib "user32" (ByRef plii As LASTINPUTINFO) As Integer
Private Structure LASTINPUTINFO
Public cbSize As Integer
Public dwTime As Integer
End Structure
Private lii As New LASTINPUTINFO

Public Function iIdleTime() Function iIdleTime() As Integer
Try
lii.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(lii)
If GetLastInputInfo(lii) <> 0 Then
Dim el As Integer = lii.dwTime
Dim ui As Integer = (Environment.TickCount - el)
' overflow handling
If (ui < 0) Then
ui = ui + Integer.MaxValue + 1
End If
Debug.Print(ui)
iIdleTime = ui
End If
Catch ex As Exception
Debug.Print(ex.Message.ToString)
End Try
End Function
#End Region
Private mForm As Form
Private mFunc0 As DFunc0
Private miTimerInterval As Integer
Private miReminderTime As Integer = 10
Private mbHasMainFormOnly As Boolean = False
Private mbIsFuncEnd As Boolean = False
Private mbAvtiveForm As Boolean = False

Protected lForm As New List() lForm As New List(Of Form)

Public Sub SetMainForm() Sub SetMainForm(ByVal pform As Form)
Try
Me.mForm = pform
AddHandler pform.Disposed, AddressOf MainFormDisposed
Me.SetFrom(pform)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetFrom() Sub SetFrom(ByVal pform As Form)
Try
lForm.Add(pform)
If Me.mbHasMainFormOnly Then
AddHandler pform.Activated, AddressOf FormActivated
AddHandler pform.Deactivate, AddressOf FormDeactivated
End If
AddHandler pform.Disposed, AddressOf FormDisposed
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

" SetParameter "#Region " SetParameter "

Public Sub HasMainFormOnly() Sub HasMainFormOnly()
Try
mbHasMainFormOnly = True
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetFunc() Sub SetFunc(ByVal pFunc0 As DFunc0)
Try
' Set logoff function
mFunc0 = pFunc0
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetTimerInterval() Sub SetTimerInterval(ByVal piSecond As Integer)
Try
miTimerInterval = piSecond * 1000
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetReminderTime() Sub SetReminderTime(ByVal piSecond As Integer)
Try
miReminderTime = piSecond
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
#End Region

Public Sub StartTimer() Sub StartTimer()
Try
nsThread.ParallelFunctionWithOutInvoke(AddressOf Me.CheckingIdle)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Private Sub CheckingIdle() Sub CheckingIdle()
Try
Dim liSleepTime As Integer = 1000
While Not mbIsFuncEnd
Thread.Sleep(liSleepTime)
If mbHasMainFormOnly Then
If mbAvtiveForm Then
' one autologoff for one form.
If iIdleTime() >= miTimerInterval Then
nsThread.InvokeFunction(mForm, AddressOf Me.TimerTick)
End If
End If
Else
' one autologoff for whole system.
If iIdleTime() >= miTimerInterval Then
nsThread.InvokeFunction(mForm, AddressOf Me.TimerTick)
End If
End If
End While
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Private Sub TimerTick() Sub TimerTick()
Try
If Not mbIsFuncEnd Then
If Me.bContinue(True) = True Then Exit Sub
End If
If Me.mFunc0 Is Nothing Then
Call RemoveForm()
Me.mForm.Close()
Else
Call RemoveForm()
Me.mFunc0.Invoke()
If Me.mbHasMainFormOnly Then
RemoveHandler mForm.Activated, AddressOf FormActivated
RemoveHandler mForm.Deactivate, AddressOf FormDeactivated
End If
RemoveHandler mForm.Disposed, AddressOf FormDisposed
RemoveHandler mForm.Disposed, AddressOf MainFormDisposed
End If
Me.mbIsFuncEnd = True
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Private Sub RemoveForm() Sub RemoveForm()
' Remove all sub form in the form list.
Dim i As Integer = lForm.Count - 1
While i >= 1
lForm.Item(i).Close()
lForm.RemoveAt(i)
i = i - 1
End While
End Sub

Protected Function bContinue() Function bContinue(ByVal pbLogOff As Boolean) As Boolean
bContinue = False
Try
' pop up reminder screen to ask user continue or not.
Using scrAutoClose As New frmAutoClose
scrAutoClose.mbLogOff = pbLogOff
scrAutoClose.miReminderTime = miReminderTime
scrAutoClose.ShowDialog()
If scrAutoClose.mbContinue = True Then
bContinue = True
End If
End Using
Catch Err As Exception
Debug.Print(Me.GetType.ToString & " - " & Err.ToString)
End Try
End Function

" Handler "#Region " Handler "

Protected Sub FormActivated() Sub FormActivated(ByVal sender As Object, ByVal e As System.EventArgs)
Try
mbAvtiveForm = True
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Protected Sub FormDeactivated() Sub FormDeactivated(ByVal sender As Object, ByVal e As System.EventArgs)
Try
mbAvtiveForm = False
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Protected Sub FormDisposed() Sub FormDisposed(ByVal sender As Object, ByVal e As System.EventArgs)
Try
lForm.Remove(sender)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Protected Sub MainFormDisposed() Sub MainFormDisposed(ByVal sender As Object, ByVal e As System.EventArgs)
Try
' set this to true to stop the thread timer.
Me.mbIsFuncEnd = True
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
#End Region
End Class
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life