-
Dec 31st, 2019, 08:02 AM
#1
Thread Starter
Member
form_Activate firing when form is not active / implementing an auto log out box
I support an application which has user logins to control access. When the app loads it displays a home screen, then on top loads a login form modally to prevent the user from switching to the home screen without logging in. Given valid credentials, the login form then closes to enable the app to be used.
The client has requested that it auto log out after a period of time, I have implemented this through a timer which starts when Form_Activate fires, then is disabled on Form_Deactivate.
The effect should be that when left on the home screen it will log out after 1 minute, but as soon as anything else is done within the app the auto log out will be cancelled to prevent a user being logged out whilst using the app.
This does work 99% of the time (and has worked 100% of the time on my test rig) but in production I am having a small number of occasions where users are being logged out in the middle of transactions. I've even had a copy of runtime error 400 crashes as the login screen has tried to reload when already displayed.
It would seem that in certain circumstances either Form_Activate on the home screen fires even when it is not the top most form, or Form_Deactive is failing to fire and cancel the timer (I'm not sure which or if it is both). All forms in the app are called modally so it should not become active except when it is supposed to.
Any idea on what could be causing this and/or a better way of implementing an auto logout?
Last edited by chris223b; Dec 31st, 2019 at 08:10 AM.
-
Feb 4th, 2020, 01:19 PM
#2
Re: form_Activate firing when form is not active / implementing an auto log out box
Hello, I don't know if you already resolved this, but I suggest a protection: before logging out, check whether the main form is the one active:
Code:
Private Sub tmrLogout_Timer()
tmrLogout.Enabled = False
If Screen.ActiveForm Is frmMain then
Logout
End If
End Sub
-
Feb 4th, 2020, 02:34 PM
#3
Re: form_Activate firing when form is not active / implementing an auto log out box
It's been a while, but if I remember right, the Actvate event doesn't necissarily fire when the app regains focus... just when that form gets focus within the app.
In other words, if I hae an app with two forms, Form1 and Form2, and I show them both... If I click Form2, it's activate event should fire. Clicking back to Form1 will fire the activate event for that form... If I then click off the app... then click Form1 again... it's activate even doesn't fire, because within the context of the app, it already had focus. But like I said, it's been a while so I may be mis-remembering. I do remember though that using the Activate event is very tricky.
-tg
-
Feb 4th, 2020, 02:36 PM
#4
Re: form_Activate firing when form is not active / implementing an auto log out box
Originally Posted by chris223b
The client has requested that it auto log out after a period of time, I have implemented this through a timer which starts when Form_Activate fires, then is disabled on Form_Deactivate.
Shouldn't that be the other way around? Stop the timer when tehapp gets focus, and enable it when it doesn't?
-tg
-
Feb 4th, 2020, 03:28 PM
#5
Re: form_Activate firing when form is not active / implementing an auto log out box
Originally Posted by techgnome
Shouldn't that be the other way around? Stop the timer when tehapp gets focus, and enable it when it doesn't?
-tg
As I understood it, it works like this:
when the main form gets the focus (activated), the user have one minute to open a secondary form, if within that minute she does nothing, then the programs logs of the user.
Then the OP sets a timer in the Activate event and disable it in the Deactivate event of the main form.
The problem that he is facing is that it seems that in some unknown condition, the Activate event of the form is fired when the user has other forms opened.
It is fired only the Activate event, but not de Deactivate.
About that the form events are not fired when the user switches to another app, it should not interfere with that logic.
-
Feb 4th, 2020, 04:31 PM
#6
Thread Starter
Member
Re: form_Activate firing when form is not active / implementing an auto log out box
Originally Posted by Eduardo-
Hello, I don't know if you already resolved this, but I suggest a protection: before logging out, check whether the main form is the one active:
Code:
Private Sub tmrLogout_Timer()
tmrLogout.Enabled = False
If Screen.ActiveForm Is frmMain then
Logout
End If
End Sub
TBH I had 'parked' this query for a while whilst dealing with other things they want done, the auto log out feature wasn't working reliably in production so I just removed all the auto log out code in the production build for now. Thanks though, I think protecting frmMain so that a check is made that it is active will be the way to go when attempting to re-introduce this feature...assuming that it doesn't turn out that whatever was firing the Activate event doesn't also cause Screen.ActiveForm to also become true for frmMain when it isn't. Thank you both for your advice though
-
Feb 4th, 2020, 04:40 PM
#7
Re: form_Activate firing when form is not active / implementing an auto log out box
Originally Posted by chris223b
assuming that it doesn't turn out that whatever was firing the Activate event doesn't also cause Screen.ActiveForm to also become true for frmMain when it isn't.
Another security measure could be to check Forms.Count.
I suppose that when the main form is active, it has to be the only form loaded.
Code:
If (Screen.ActiveForm is Me) And (Forms.Count = 1) Then
Last edited by Eduardo-; Feb 4th, 2020 at 04:45 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|