Calling WebPage.Show() from a Session

I’m using a Timer created in new sessions to log-off users based on some conditions. When the timer fires everything goes well, except when I try to call “LoginPage.Show()”, which throws a NiObjectException.

Is it possible to direct a user to a WebPage via Methods in a Session?

Is LoginPage an Implicit Instance?

Make sure the Super of the timer is WebTimer so it gets properly associated with the session.

Just out of curiosity, does this exception cause the debugger to break on the offending line?

Hi Greg, when I swap over from instantiating a ‘Timer’ to ‘WebTimer’…

auto_logoff_timer = New WebTimer

…I now get an error that the ‘constructor of this class is protected’. Is there a trick to instantiating a WebTimer as a Property of Session?

Hi Hector, yes… all my WebPages are by default toggled to ‘Implicit Instance’. However this isn’t a setting I’ve ever (yet) had to change. :thinking:

Greg: Yes it does break on the exception in the IDE, here’s where it snags:

Private Sub auto_logoff_timer_action(Sender As Timer)

  Session.AlreadyLoggedIn = FALSE
  LoginPage.Show() 'breaks on this line here...
End Sub

Update: I think WebTimer has to be created in a Page and not Session, so I’m rolling back to simple ‘Timer’ in the Session property. Still caused debugger to catch exception when trying to call .Show() of a WebPage.

Turn off Implicit Instance : it’s supposed to make life easier, but often makes it harder.
Instead, do this:

Private Sub auto_logoff_timer_action(Sender As WebTimer)

  Session.AlreadyLoggedIn = FALSE
  dim lp as new LoginPage
End Sub

Also, you do need to use a WebTimer so it’s running in the Session.

A question about WebTimer… does it require to be created in a WebPage (vs. in a Session), and doesn’t it fall out of scope whenever a page is navigated away from? (e.g. when Close is called on a Page)

Good question: In theory I think you could create a new WebTimer as a session variable, but I’ve never tried it.

If you can’t get WebTimer to work as a session variable, here’s another way you can do it - add this function to your app:

Private Sub ShowLoginPage(whichSession as variant)
  // shows a loginPage for an active session
  #Pragma DisableBackgroundTasks
  For i As Integer = 0 To App.SessionCount - 1
    var sess as session = App.SessionAt(i)
    if sess <> whichSession then
    end if
    // Without creating a WebSessionContext here, creating
    // the page would fail triggering a SessionNotAvailableException.
    var context As New WebSessionContext(sess)

    // Because we have a context, we can create a new
    // WebPage and assign it to the session.
    var wp as new LoginPage
    context.Session.CurrentPage = wp
    wp.Show  // this line may not be necessary if wp.visible = true
End Sub

To make this happen, you can put this code in a session event:

Timer.CallLater(1, addressOf App.ShowLoginPage, session) // this function will be called on the App's event loop, not in the session's

Could you verify for me that you are getting a NilObjectException? Or is it something else?

Hi Greg,

Here it is :slight_smile:

Your property must also be a WebTimer not Timer