Prince of Dhump

Hi Guys,

I'm developing an application which consists of some forms. I would need to put a timer on the application so if there is no activity then in 5 minutes the application should display the login screen again.

I'm thinking of placing a public method in a module and then on each form try to run this method, this method will contain the timer but how would i go for this logic to record any activity on the forms

I'm stuck with this and can't find any answer

Can you all please help me..

Thanks in advance.




Re: Windows Forms General Timer and windows forms

nobugz

Check this thread for a solution.





Re: Windows Forms General Timer and windows forms

Prince of Dhump

Hi Hans,

I don't have a main form and there are 8-9 forms in the application itself.

Should i put this code in each form Also the code is in C# which i unfortunately have no idea......... can you help me in VB.net


Thanks






Re: Windows Forms General Timer and windows forms

nobugz

I posted a VB.NET version of the code in the same thread. A good place to put this code would be your login form. Just hide it after a successful login, call ShowDialog() to bring it back up when the timer expires. You just need to make sure to close the form when you exit your application.





Re: Windows Forms General Timer and windows forms

Prince of Dhump

Hi Hans,

Sorry to bother you again... but i'm getting an exception when i'm putting in your code.

Here is what i put in:

Code Snippet

Public Class frmTemp

Implements IMessageFilter

Dim Timer1 As System.Windows.Forms.Timer

Public Sub New()

InitializeComponent()

AddHandler Timer1.Tick, AddressOf AppInactive

Timer1.Interval = 1000

Timer1.Enabled = True

Application.AddMessageFilter(Me)

End Sub

Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage

Dim mouse As Boolean = (m.Msg >= &H200 And m.Msg <= &H20D) Or (m.Msg >= &HA0 And m.Msg <= &HAD)

Dim kbd As Boolean = (m.Msg >= &H100 And m.Msg <= &H109)

If mouse Or kbd Then

If Not Timer1.Enabled Then Console.WriteLine("Waking up")

Timer1.Enabled = False

Timer1.Enabled = True

End If

End Function

Private Sub AppInactive(ByVal sender As Object, ByVal e As EventArgs)

Timer1.Enabled = False

Console.WriteLine("Going to sleep")

End Sub

End Class

The error is
An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

What did i do wrong


Thanks






Re: Windows Forms General Timer and windows forms

nobugz

You didn't create the Timer1 object. Change its declaration like this:

Dim Timer1 As New Timer

Or just drop a timer onto the form from the toolbox.





Re: Windows Forms General Timer and windows forms

Prince of Dhump

Thanks Hans...... Sorry for the stupid mistake






Re: Windows Forms General Timer and windows forms

Prince of Dhump

Hi Hans,


Sorry again but i need some more help from you.

I was trying to work out with your code but I think i'm not an expert user like you so i'm getting some problems.

When i put in this code:

Code Snippet

Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage

Dim mouse As Boolean = (m.Msg >= &H200 And m.Msg <= &H20D) Or (m.Msg >= &HA0 And m.Msg <= &HAD)

Dim kbd As Boolean = (m.Msg >= &H100 And m.Msg <= &H109)

If mouse Or kbd Then

If Not Timer1.Enabled Then MessageBox.Show("Waking up")

Me.Show()

Timer1.Enabled = False

Timer1.Enabled = True

End If

End Function

Private Sub AppInactive(ByVal sender As Object, ByVal e As EventArgs)

Timer1.Enabled = False

MessageBox.Show("Going to sleep")

Me.Hide()

End Sub

I don't know but when its hide, it doesn't come up. This is the login form and the other thing which i don't understand is what can i do to have it come up when ever the 5 minutes duration is expired on any form in the application, All of the forms in the application are individual, The only form which is the main form is an startup form but that is not an MDI form as well, Would it be okay to make an MDI form form and then put the code over there or would it work like this.

Thanks in advance






Re: Windows Forms General Timer and windows forms

nobugz

Not sure what your problem might be but you got the logic backwards. You want to display the login form when the application goes idle, not hide it. You want to hide it when the user successfully logged in. Add a Login and Cancel button to your login form. You'll want to display the login dialog when you first start up:

Public Class Form1
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim login As New Form2
If login.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Application.Exit()
End Sub
End Class

This would be the login form:

Public Class Form2
Implements IMessageFilter
Dim Timer1 As New Timer

Public Sub New()
InitializeComponent()
AddHandler Timer1.Tick, AddressOf AppInactive
Timer1.Interval = 1000
Timer1.Enabled = False
Application.AddMessageFilter(Me)
End Sub

Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Dim mouse As Boolean = (m.Msg >= &H200 And m.Msg <= &H20D) Or (m.Msg >= &HA0 And m.Msg <= &HAD)
Dim kbd As Boolean = (m.Msg >= &H100 And m.Msg <= &H109)
If Timer1.Enabled And (mouse Or kbd) Then
'--- Activity seen, restart the timer
Timer1.Enabled = False
Timer1.Enabled = True
End If
End Function

Private Sub AppInactive(ByVal sender As Object, ByVal e As EventArgs)
'--- No activity, display the dialog
Timer1.Enabled = False
If Me.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Application.Exit()
End Sub

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
'--- Verify login credentials
'...
Me.DialogResult = Windows.Forms.DialogResult.OK
Timer1.Enabled = True ' Start monitoring activity
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
End Class





Re: Windows Forms General Timer and windows forms

nobugz

Not sure what your problem might be but you got the logic backwards. You want to display the login form when the application goes idle, not hide it. You want to hide it when the user successfully logged in. Add a Login and Cancel button to your login form. You'll want to display the login dialog when you first start up:

Public Class Form1
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim login As New Form2
If login.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Application.Exit()
End Sub
End Class

This would be the login form:

Public Class Form2
Implements IMessageFilter
Dim Timer1 As New Timer

Public Sub New()
InitializeComponent()
AddHandler Timer1.Tick, AddressOf AppInactive
Timer1.Interval = 1000
Timer1.Enabled = False
Application.AddMessageFilter(Me)
End Sub

Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Dim mouse As Boolean = (m.Msg >= &H200 And m.Msg <= &H20D) Or (m.Msg >= &HA0 And m.Msg <= &HAD)
Dim kbd As Boolean = (m.Msg >= &H100 And m.Msg <= &H109)
If Timer1.Enabled And (mouse Or kbd) Then
'--- Activity seen, restart the timer
Timer1.Enabled = False
Timer1.Enabled = True
End If
End Function

Private Sub AppInactive(ByVal sender As Object, ByVal e As EventArgs)
'--- No activity, display the dialog
Timer1.Enabled = False
If Me.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Application.Exit()
End Sub

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
'--- Verify login credentials
'...
Me.DialogResult = Windows.Forms.DialogResult.OK
Timer1.Enabled = True ' Start monitoring activity
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
End Class






Re: Windows Forms General Timer and windows forms

Prince of Dhump

Hi Hans,

Thank you very very much for your reply. Sorry that i couldn't reply yesterday as I left my desk. I've tried to put the code in today. It works almost fine but only 2 things going wrong.......... (I know i'm pain in the head) but i couldn't figure it out.

Somehow the login form comes up 2nd time when i click on manual login or login........ i first thought it is coming up because of timer1.interval as the value is only 1 second so i increased it but it does come up 2nd time. This is only happening at the start of the application not any other time.

Secondly on my startup form I have some code on form_closing which would just confirm the closing of form however if i click cancel then the login form is closed and the timer is off. Now i do have these sort of closing confirmation almost in every form..... What would be the best trick to do it

Thanks again.






Re: Windows Forms General Timer and windows forms

Prince of Dhump

Hi Hans,

Manage to crack the 2nd time displaying of the login form so the only thing remain is the closing confirmation.

Thanks