Global web threads

Hi Everyone

I am new to the forum and newish to Xojo (played with RB some years back). I am currently playing with the trial version to see if I am going to buy the Pro version but have come across and issue that I cant seem to find an answer for and I was wondering if someone might have a suggestion.

I am working on a Web app (using Windows to code it) and am getting my head around the threads to be able to run background tasks for a user session but I would like to be able to run global tasks that have an effect on all sessions. So for example every 10 minutes I want to poll a database table to see if any new items have been added to it and if so then I want to set a variable in each of the users session to tell it that something needs to be done e.g. display a dialog telling them that things have been updated. If I could get a variable to change or change a global variable in some way then I could have a timer object for each session that checked the value of the variable and react accordingly.

I am sure someone will have a much cleaner / better solution to this but hopefully that gives the general gist of the idea.

Thanks

Nathan

Look at the “Timer” class (not the “WebTimer” class). Perhaps add a Timer property to each Session:

property tDoSomething as Timer

In Session.Open:

tDoSomething = new Timer tDoSomething.mode = 2 tDoSomething.period = 10 * 60 * 1000 // (10 minutes) AddHandler tDoSomething.Action, AddressOf DoSomething_Action

DoSomething_Action(t as Timer) would be a Session method that does your periodic action.

HTH, -Brad

You can also have the thread itself loop through the sessions and take action via WebApplication.SessionAtIndex.

I believe with either solution you will have to setup the WebSessionContext before calling any methods which might alter the state of open web pages, or which might call Session to get the active session.

Another variation would be to have your thread simply update a property on each session, and have a WebTimer check said property and take any necessary action. You do not have to worry about the WebSessionContext when a WebTimer event fires. Keep in mind a WebTimer will continue to fire if the page it’s on is still open but not in front.

Depending on how complex the thread’s behavior is, you may also have to coordinate using CriticalSection or Semaphores. If you keep it separated the way you described it (i.e. thread marks a variable or property; something else takes action later) you should be fine.

Hi Brad

Thanks for the quick response.

I will have to read up on the difference between a Timer and WebTimer but would that not create the same even multiple times? What I need is one event that checks the db that runs every 10 minutes (this is the grunt part that the user never sees). What I then need to do is tell all the sessions when something has changed which is why I thought of then having a local session timer event that runs ever 1 minute to check if the grunt part had found any changes.

Nathan

Depending on the complexity of “tell all the sessions when something has changed”, you could just do this from the thread on the 10min check. Let’s say you have a label that displays the latest change, something like “X New Messages”, on the user’s web page. Loop through the sessions via SessionAtIndex, setup the WebSessionContext, and update the label text.

For complex activity I would probably use a Timer or WebTimer, one for each session.

A WebTimer is a client-side Javascript Timer that sends an event back to your app periodically. You’ll need an active user Session for it to fire periodically. Timer is server-side and would fire regardless of if you have user sessions – that is, if you has it as an App property.

-Brad

Brilliant, thanks guys for your help. I think the combination of a web timer and a timer will work perfectly and using a property to store a timestamp of when the info was last update should work perfectly for what I need.

What I love about Xojo is the number of ways to achieve the desired results.

I am just starting to read back through the last few years of xDev to try and learn tip and tricks etc and subscribed to BKeeney videos.

Does anyone know of a really good list of plugins as the one on the Xojo site does need appear to be complete and I would love to look at plugins in general but my current project is for web based plugins.

Thanks

Nathan

[quote=72229:@Nathan Wright]Does anyone know of a really good list of plugins as the one on the Xojo site does need appear to be complete and I would love to look at plugins in general but my current project is for web based plugins.
[/quote]

Oh man, you just joined the dark side… For web and desktop, MBS ( http://www.monkeybreadsoftware.net/ ) has whole bunch of useful stuff. If it’s in your budget, buy the compete now and enjoy what it solves for you over time. For web, I offer Studio Stable Web Essentials ( http://studiostable.com ) and Daniel Taylor offers Web Custom Controls ( http://webcustomcontrols.com/ ). Both have great uses in an actual commercial project I’m helping someone with right now.

I would second Brad’s suggestion to just get the complete MBS. Sooner or later you will be glad you did.

Another good source of plugins: Einhugur

Naturally it depends on your needs, but Dethomsoft’s dtplugins offer some nice controls for desktop apps. http://www.dethomsoft.com/index.html