Results 1 to 7 of 7

Thread: form_Activate firing when form is not active / implementing an auto log out box

  1. #1

    Thread Starter
    Member
    Join Date
    May 2018
    Posts
    51

    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.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: form_Activate firing when form is not active / implementing an auto log out box

    Quote Originally Posted by chris223b View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: form_Activate firing when form is not active / implementing an auto log out box

    Quote Originally Posted by techgnome View Post
    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.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2018
    Posts
    51

    Re: form_Activate firing when form is not active / implementing an auto log out box

    Quote Originally Posted by Eduardo- View Post
    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

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: form_Activate firing when form is not active / implementing an auto log out box

    Quote Originally Posted by chris223b View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width