Thread or Timer

In my web app I want to have a task that runs every 5 minutes and may take up to 2 minutes to complete. Is the general rule to use a thread or a timer for this type of thing?

I’d use a Timer to start a Thread.

I’d second what Kem says. The timer is always going to run in the main event loop of the app. This means that anything that might end up in a very tight loop could cause delays in the UI. So a task that takes 2 minutes to complete sounds like something that could be some intensive processing. So yeah, use a thread for that. Just keep in mind that if the thread needs to update the UI, you cannot do that directly. You’ll need to save the desired data to properties and then use timers to update the UI with the values of those properties. Or use Christian’s thread safe classes.

Why is just when I think I have my head around Xojo someone comes along with something totally new :wink: (PS: grabbing a glass of wine as I think the answer may get complicated !!!)

So why would you use a timer to start the thread when you can just start a thread directly from a method I thought?

Is this true with a web app where the UI is not really being updated on the location of the server but remotely and therefore the server is just handling it?

You have code that is going to take two minutes to run, and that will tie up the app. That code should be in a Thread.

You need that code to repeat every 5 minutes, so you can launch that Thread through a Timer. Each class does what it was intended to do.

But I question what it’s doing that will take two minutes. Can that be optimized?

Thanks Kem, that now makes sense.

The two minutes is a worse case but I was using it to make a point. It will be collecting data from multiple remote servers, cleansing the data, checking for error, sending emails to people depending on outcome etc.

Whilst typing this I am thinking that it may be better to put this out to a console app like I have done with other stuff rather than having it in the web app which would then get rid of the need for the timer etc.

Yes, that makes more sense.