Application Timeout Required

  • Thread starter Thread starter Gerrit
  • Start date Start date
* "Gerrit said:
System.Windows.Forms.Timer

Yep. Add a timer that checks the current time every second, for
example, and calculates the difference to the last time the user worked
with the software.
 
I guess that depends on your app and what the user does within it.

You could attach to the Application.Idle event, check the active control and
store it. Set the timer to fire every 20 minutes and reset it when the "new"
activecontrol is not the one you have stored (via the Application.Idle
event). But there are probably more elegant ways to do it.
Another idea would be track when the user uses the keyboard (use the form's
KeyPreview) and/or mouse.

HTH,

Gerrit

Alok said:
I actually had thinking about this.
But, how do you know, when the user last worked ????
Do you mean to say that i trap all user movements like keyboard, mouse
etc... ???

TIA :)
 
Set "parent" form KeyPreview = True and use it's KeyPreview event. That
should work quite well in MDI apps. You may need to handle any dialog
windows in some way, thought.

Gerrit
 
Hi,
My windows application needs a time out functionality.
After a specified time interval (say 20 mins) of inactivity, I need to show
the enter password screen.

I tried the Application_Idle event but that didn't work out well.

Could somebody give any suggestions ?

TIA :)
 
I actually had thinking about this.
But, how do you know, when the user last worked ????
Do you mean to say that i trap all user movements like keyboard, mouse
etc... ???

TIA :)
 
On one of the other post said implementing the IMessageFilter and re-setting
the timer.
But, thing is that probably IDle event also goes thru' the Message Filter.
hence that doesn't work...

Considering the Keyboard and mouse, with a full-blown Mdi app, it would be a
monstrous effort to acheive that.
Does anybody have a plug-in / control that can be bought ?? Even a COM
component would work, I suppose !!!

TIA :)

Gerrit said:
I guess that depends on your app and what the user does within it.

You could attach to the Application.Idle event, check the active control and
store it. Set the timer to fire every 20 minutes and reset it when the "new"
activecontrol is not the one you have stored (via the Application.Idle
event). But there are probably more elegant ways to do it.
Another idea would be track when the user uses the keyboard (use the form's
KeyPreview) and/or mouse.

HTH,

Gerrit
 
The KeyPreview event would handle the child forms key events also ??
I don't think so. It would handle only for the current form.

What i was thinking of was like this:
1. have a global variable as a counter to keep track of time elapsed.
2. Some one continuously polls the message pump to check if there are any
messages. if there are no messages, counter is incremented. If there is any
message, the counter is reset.
3. Only problem is once you do the Application.Run(), it would be a blocking
call. Hence, the "Someone" who polls the pump, needs to be in a separate
context.
4. Now, my question is, how to set up two pumps / UI threads in a single
process so that one thread polls if there is a message in another pump ???

TIA :)
 
the background thread listens on the main UI thread (that contains the
message pump). hence I need to start two UI threads.

Gerrit Schunk said:
AFAIK, Setting KeyPreview = True on the parent form should allow you to
capture all key strokes using the parent form's events.
 
Here is something I have been playing around with to accomplish this. It
has not been thoroughly tested in an application. Maybe it will give you
some ideas. It uses a timer and the application message filter.

--Mike

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(376, 326)

Me.Name = "Form1"

Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AppIdle.Init()

AppIdle.StartTimer()

Application.AddMessageFilter(New TestMessageFilter)

End Sub

End Class

Public Class AppIdle

Private Shared tmr As Timer

Public Shared Sub Init()

tmr = New Timer

tmr.Interval = 10000 'Amount of idle time before timing out (in ms)

tmr.Start()

AddHandler tmr.Tick, AddressOf fire

End Sub

Public Shared Sub StartTimer()

tmr.Start()

End Sub

Public Shared Sub StopTimer()

tmr.Stop()

End Sub

Public Shared Sub ResetTimer()

tmr.Stop()

tmr.Start()

End Sub

Private Shared Sub fire(ByVal sender As Object, ByVal e As EventArgs)

tmr.Stop()

MessageBox.Show("Timeout!")

End Sub

End Class

Public Class TestMessageFilter

Implements IMessageFilter

Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) _

As Boolean Implements IMessageFilter.PreFilterMessage

Debug.WriteLine(m.ToString)

Debug.WriteLine(m.Msg.ToString)

If m.Msg <> 275 Then 'ignore timer messages WM_TIMER

AppIdle.ResetTimer()

End If

Return False

End Function

End Class
 
I don't think that you can have two threads for one and the same the UI. Try using the Application.Idle event instead.

Gerrit
 
Back
Top