time-out form

D

Dayne

I need to implement a time-out form under .net(vb.net). So far I have been
able to creat a child thread which checks to see when the application
exceeds a set idle time. .. My problem is the fact the once I call a
callback function which invokes the time-out form , there is no way for me
to exit the child thread. The time-out form has 2 options: to restart the
program or to go back to the last viewed page.My implementation of the child
thread looks something like this .

Public Class IdleTimer

Implements System.IDisposable

Public Structure LastInputInfo

Public cbsize As Int32

Public dwtime As Int32

End Structure

Public Delegate Sub IdleTimeCallback()

Public callback As IdleTimeCallback

'Public callback As WaitOrTimerCallback

Public WithEvents frm As New Form

Public Shared ThreadCount As Integer = 0

Public MaxIdleTime As Integer = 30000

Private _idleTimerThread As Thread

Private _parentThread As Thread

'Public ReadOnly Property IdleTimerRunning() As Boolean

' Get

' Return Me._idleTimerRunning

' End Get

'End Property



Declare Auto Function GetLastInputInfo Lib "user32.dll" (ByRef value As
LastInputInfo) As LastInputInfo

Sub New(ByVal mit As Integer, ByRef cb As IdleTimeCallback)

Me.MaxIdleTime = mit

Me.callback = cb

Me.ThreadCount += 1

Me._parentThread = Thread.CurrentThread

End Sub

Public Sub StartIdleTimer()

'If (Me._idleTimerRunning = True) Then

' Throw New Exception("Timer is already running")

'End If

'Me._idleTimerRunning = True

Me._idleTimerThread = New Thread(New ThreadStart(AddressOf
IdleTimerThreadMain))

Me._idleTimerThread.Name = "IdleTimerThread[" & Me.ThreadCount & "]"

Me._idleTimerThread.Start()

'Me._idleTimerThread.Join()

End Sub

Public Sub StopIdleTimer()

' If (Me._idleTimerRunning = False) Then

' Throw New Exception("Timer is not running")

' Exit Sub

' End If

Me._idleTimerThread.Abort()

'Me._idleTimerRunning = False

End Sub

Private Sub IdleTimerThreadMain()

Dim lii As LastInputInfo

lii.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(lii)

Dim currentTicks As Integer

Dim lastInputTicks As Integer

Dim idleTicks As Integer

While (True)

Me.GetLastInputInfo(lii)

currentTicks = System.Environment.TickCount

lastInputTicks = lii.dwtime

idleTicks = currentTicks - lastInputTicks

Dim debug As StringBuilder = New StringBuilder

debug.AppendFormat("Current tick = {0}, ", currentTicks)

debug.AppendFormat("Last input tick = {0}, ", lastInputTicks)

debug.AppendFormat("Differenc = {0}, ", idleTicks)

System.Diagnostics.Debug.WriteLine(debug.ToString)

If (idleTicks >= Me.MaxIdleTime) Then

Exit While

End If

System.Threading.Thread.Sleep(1000)

End While

Me.callback()

Application.Run()

End Sub



Public Sub Dispose() Implements System.IDisposable.Dispose

System.Diagnostics.Debug.WriteLine("Timer diposed")

End Sub

Protected Overrides Sub Finalize()

System.Diagnostics.Debug.WriteLine("Timer finalize")

End Sub



End Class


Please help

Dayne
 
C

Cor Ligthert

Dayne,

Does this sample I once made maybe help you in this.
http://groups.google.com/[email protected]

I hope this helps?

Cor

Dayne said:
I need to implement a time-out form under .net(vb.net). So far I have been
able to creat a child thread which checks to see when the application
exceeds a set idle time. .. My problem is the fact the once I call a
callback function which invokes the time-out form , there is no way for me
to exit the child thread. The time-out form has 2 options: to restart the
program or to go back to the last viewed page.My implementation of the
child
thread looks something like this .

Public Class IdleTimer

Implements System.IDisposable

Public Structure LastInputInfo

Public cbsize As Int32

Public dwtime As Int32

End Structure

Public Delegate Sub IdleTimeCallback()

Public callback As IdleTimeCallback

'Public callback As WaitOrTimerCallback

Public WithEvents frm As New Form

Public Shared ThreadCount As Integer = 0

Public MaxIdleTime As Integer = 30000

Private _idleTimerThread As Thread

Private _parentThread As Thread

'Public ReadOnly Property IdleTimerRunning() As Boolean

' Get

' Return Me._idleTimerRunning

' End Get

'End Property



Declare Auto Function GetLastInputInfo Lib "user32.dll" (ByRef value As
LastInputInfo) As LastInputInfo

Sub New(ByVal mit As Integer, ByRef cb As IdleTimeCallback)

Me.MaxIdleTime = mit

Me.callback = cb

Me.ThreadCount += 1

Me._parentThread = Thread.CurrentThread

End Sub

Public Sub StartIdleTimer()

'If (Me._idleTimerRunning = True) Then

' Throw New Exception("Timer is already running")

'End If

'Me._idleTimerRunning = True

Me._idleTimerThread = New Thread(New ThreadStart(AddressOf
IdleTimerThreadMain))

Me._idleTimerThread.Name = "IdleTimerThread[" & Me.ThreadCount & "]"

Me._idleTimerThread.Start()

'Me._idleTimerThread.Join()

End Sub

Public Sub StopIdleTimer()

' If (Me._idleTimerRunning = False) Then

' Throw New Exception("Timer is not running")

' Exit Sub

' End If

Me._idleTimerThread.Abort()

'Me._idleTimerRunning = False

End Sub

Private Sub IdleTimerThreadMain()

Dim lii As LastInputInfo

lii.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(lii)

Dim currentTicks As Integer

Dim lastInputTicks As Integer

Dim idleTicks As Integer

While (True)

Me.GetLastInputInfo(lii)

currentTicks = System.Environment.TickCount

lastInputTicks = lii.dwtime

idleTicks = currentTicks - lastInputTicks

Dim debug As StringBuilder = New StringBuilder

debug.AppendFormat("Current tick = {0}, ", currentTicks)

debug.AppendFormat("Last input tick = {0}, ", lastInputTicks)

debug.AppendFormat("Differenc = {0}, ", idleTicks)

System.Diagnostics.Debug.WriteLine(debug.ToString)

If (idleTicks >= Me.MaxIdleTime) Then

Exit While

End If

System.Threading.Thread.Sleep(1000)

End While

Me.callback()

Application.Run()

End Sub



Public Sub Dispose() Implements System.IDisposable.Dispose

System.Diagnostics.Debug.WriteLine("Timer diposed")

End Sub

Protected Overrides Sub Finalize()

System.Diagnostics.Debug.WriteLine("Timer finalize")

End Sub



End Class


Please help

Dayne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top