How to create a background process

Jim Bufalini jim at visitrieve.com
Sat Sep 26 21:04:30 EDT 2009


Josep wrote:

> Hi,
> 
> I experimented with the send in time command.
> I need to program some tasks to run at fixed time and days. Like
> schedule
> a task for a backup for example.
> How can control this? Any experience?
> And how lunch these task in the way that don't stop or delay the
> app and/or system?
> 
> My idea is to program the time that the task must
> be run. When the app is lunched I check thr task for day and
> "send in time" the task.
> The task will be executed by a standalone stack.
> The task are backups and calculate grand totals.
> 
> By other way a lunch speak messages to the user, but if one message is
> runnig
> and one second message start, the first is stoped.
> How wait until the revspeak finish before start the next?
> I tryed from a custom message and using wait for message but doesn't
> work.

When I've done this, I write one handler that when an app is launched reads
in the tasks to be performed from a db (it could read them in from a file).
If any tasks are over-due, it performs them either immediately or
recalculates a new time for the task (as in a backup that should only be
performed at night) and saves it off to disk as well as a custom property,
so it doesn't have to keep checking the disk.

It then sorts by time and if the next task is to be performed in say 6
hours, it sends to itself in 5 hours and 59 minutes. ;-) When it is called,
it first checks the custom property and if there are tasks, it uses the next
one at the appropriate time and if it is empty, it means the app has just
been launched, so it then reads from disk as above.

Once it performs a task, it calculates the next time, saves it off to disk
and the custom property. It then looks at the next closest task to now and
if that is say 24 hours away, it sends to itself in 23 hrs and 59 minutes...

In this fashion, no task is ever missed and the handler only runs when it
has to and it only has to check the disk once on launch, although it writes
to disk whenever it performs a task. If you are only running on Windows, you
can use the Registry for saving off pending tasks.

All this assumes that your app is usually running. You can do this by
installing your app to always load and run invisibly on system boot or use
Richard's suggestion of having a system task scheduler launch the app.

There is one other element you need. Assuming you have a preferences screen
and the user can change the time of a scheduled task, you need that screen
to write the change both to disk/registry and the custom property. You then
need a setProp handler that when the customProperty changes, wakes up the
background task handler, so it can recalculate when its next task is. If it
is earlier than previously calculated, it deletes its previous message from
the pendingMessages and does a new send to itself. To cancel a pending
message you can use:

IF "myHandlerName" is in the pendingmessages THEN
      get lineoffset("myHandlerName",pendingmessages())
      cancel item 1 of line it of the pendingmessages
END IF

Then just do a new send in time.

Lastly, when you have a setProp/getProp, always remember to turn off
messages before the background task handler writes to the custom property or
you will find yourself in an infinite loop. You turn messages back on after
the write.

Aloha from Hawaii,

Jim Bufalini







More information about the use-livecode mailing list