Xojo 2019R1.1 -Cannot call session method from a webpage method but can from a button on a webpage

Hello all,

I’m trying to put this code, that works in a Button into a Method

Sub Action() Handles Action
  Session.ShowfrmMain
   
End Sub

That same code will not work in a method

Public Sub ShowMainPage()
  Session.ShowfrmMain


End Sub

The second one always gives a Nil object error… then the app shuts down

Any ideas how to get around this?
Tim

Has the Session fully opened when you call it from the WebPage Method? Try putting the call into a Timer to ensure it is all up and running.

A timer won’t work any better. The problem you are having is where the method is being called from. Things like Sockets and Timers run their events on the main thread and sessions don’t.

Remember, each time an event comes in from a browser, the app starts a new WebThread for the execution of that code and temporarily connects it to a WebSession. That way, the framework knows which session that the code running within that thread should run on. The main thread has no such context info.

The solution is for your code to be “told” which session it belongs to by the use of a WebSessionContext object. You can create one of these yourself and then run your code or make a WebThread from that.

Hi Greg.

I saw WebSessionContext yesterday too but dunno how to implement it. The help was not much better. Can you provide a snippet?

Also, I did try implementing the command in Method on the Session, that did not work either…

I must be doing something very wrong!

Tim

The usage is as follows…

You create a WebSessionContext object at the beginning of a method and pass it the Identifier for the session you’re targeting.

Dim ctx as new WebSessionContext(id)

The context will be removed from the lookup stack when it is destroyed. Methods that run after that will have access to the session. Whose id you passed in.

Thank you Greg!

OK, that is definitely different than I expected.
How do I find the session ID?
Tim

Session objects have an Identifier property

Thanks,
Got the ID, but the code you offered throws an error on compile
Dim ctx as new WebSessionContext(id)
“There is more than 1 item with this name and it’s not clear to which this refers”

This is API1 too, just to be sure so as not to bother you too much…

Tim

Ah. API 1 requires you to pass the session itself.

Well I got it to work. Much experimenting.

Turns out in API1 that if you put a timer ON the page, it works. However if a timer is programmatically created, it does not.

Strange beast!
Thank you again everyone!
Tim

Right. When you drag out a timer to a webpage, you get a WebTimer which is “session-aware” and the events actually fire on the browser first and are sent to the app.

Timers created in code fire on the server.

FWIW, in web 2, WebTimers give you a choice.

You can emulate this by creating a Timer subclass which stores a WeakRef to the current session when it’s created and then creates a WebSessionContext in its Action event from the stored session and re-raises the event. Just make sure you check for the WeakRef being nil because if the session goes away, so should the timer.

Interesting and very good to know.

Thanks again Greg,
Tim

1 Like