Threads in Web Apps

Let’s say, for example, I want to run an emailing process (which might take a few minutes to complete), as it iterates through a SQLite db and sends a mail to each person found in the db. The process (once started) needs to remain independent of the Session and independent of the Browser (in case the net connection is broken). So this process could be run in a Thread, right?
However, the browser (if still connected) would like to receive feedback from the above thread (ie progress of the processed mails, status of the SMTPSecure socket, errors, etc).
What is the best practice for this? I could have the Thread write a text log to the server (which the App could periodically check via a webtimer). Or is there a more elegant way for the app to interrogate the Thread’s progress without interfering with the Thread’s operation?

Also, am I right in thinking that in a Web App we can use a “Timer” object for server-side processing, and a “WebTimer” object for client-side processing??

All advice gratefully received.

Last question first, yes, WebTimer for client side, Timer for server.

If you’re using CGI (or even standalone really), there’s a chance your process will be closed down and the running thread with it. So I’d recommend keeping track of progress with some sort of persistence, perhaps in that same database you’re pulling data from. That way, if you need to continue where you left off, you know who’s been emailed already.

As such, you could also periodically write progress stats to a table for quick lookup, and your timer could indeed check the status and display the current progress.

If however you’re not worried about the app closing down while processing the emails, you could stuff the progress data in an App property, maybe a Dictionary with a unique key to be checked for the current process.

Either way, you put the data somewhere that both the thread and monitoring process can access it.

If you had a dedicated server I would use a console application. If you don’t, then I would write the emails to an SQLite table and do batch processing First-In-First-Out (FIFO) then delete as they’re sent.

Hi Ian & David,

Many thanks for taking the time to share those ideas. Some good food-for-thought!

Tony